top of page

Assembly code

section .data

prompt1 db "Enter the first number: ", 0

prompt2 db "Enter the second number: ", 0

result_msg db "The square root of the sum is: ", 0

newline db "", 10, 0 ; Newline character

format_int db "%d", 0 ; Format string for integer input/output


section .bss

num1 resd 1 ; Reserve space for the first number (double word = 4 bytes)

num2 resd 1 ; Reserve space for the second number

sum resd 1 ; Reserve space for the sum

sqrt_result resd 1 ; Reserve space for the integer part of the square root


section .text

global _start


extern printf

extern scanf

extern sqrt ; Assume the C standard library's sqrt function is available


_start:

; Display prompt for the first number

mov eax, 4 ; syscall number for write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, prompt1

mov edx, 22 ; length of the prompt string

int 0x80


; Read the first number

mov eax, 3 ; syscall number for read

mov ebx, 0 ; file descriptor 0 is stdin

mov ecx, num1

mov edx, 4 ; read 4 bytes (for a dword)

int 0x80


; Display prompt for the second number

mov eax, 4 ; syscall number for write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, prompt2

mov edx, 23 ; length of the prompt string

int 0x80


; Read the second number

mov eax, 3 ; syscall number for read

mov ebx, 0 ; file descriptor 0 is stdin

mov ecx, num2

mov edx, 4 ; read 4 bytes

int 0x80


; Convert input strings to integers (assuming single-digit input for simplicity)

; For more robust input handling, you would need to implement a more complex

; conversion routine.

mov eax, [num1]

sub eax, '0' ; Convert ASCII digit to integer

mov [num1], eax


mov eax, [num2]

sub eax, '0' ; Convert ASCII digit to integer

mov [num2], eax


; Calculate the sum

mov eax, [num1]

add eax, [num2]

mov [sum], eax


; Prepare the sum as a float for the sqrt function

; We'll move the integer sum to the floating-point stack

fild dword [sum] ; Load integer into FPU stack as a double


; Calculate the square root

fsqrt ; Take the square root (result is now a float on the stack)


; Convert the floating-point result back to an integer (truncating)

fistp dword [sqrt_result] ; Store the integer part of the result


; Display the result message

mov eax, 4 ; syscall number for write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, result_msg

mov edx, 26 ; length of the result message

int 0x80


; Print the integer part of the square root

push dword [sqrt_result]

push dword format_int

call printf

add esp, 8 ; Clean up the stack


; Print a newline

mov eax, 4 ; syscall number for write

mov ebx, 1 ; file descriptor 1 is stdout

mov ecx, newline

mov edx, 2

int 0x80


; Exit the program

mov eax, 1 ; syscall number for exit

xor ebx, ebx ; exit code 0

int 0x80


Explanation:

* Sections:

* .data: Stores initialized data like prompt messages and format strings.

* .bss: Stores uninitialized data where variables will be stored.

* .text: Contains the program's executable code.

* External Functions:

* extern printf: Declares the printf function from the C standard library for formatted output.

* extern scanf: Declares the scanf function from the C standard library for formatted input (though we use direct syscalls for simplicity in this example).

* extern sqrt: Declares the sqrt function from the C standard library to calculate the square root.

* _start Label:

* This is the entry point of the program.

* Input:

* The code uses Linux syscalls (int 0x80) to display prompts (eax = 4 for write) and read input from the user (eax = 3 for read).

* It reads the input as strings and then subtracts the ASCII value of '0' to convert single-digit ASCII characters to their integer equivalents. For multi-digit numbers, you would need a more sophisticated string-to-integer conversion routine.

* Calculation:

* The two numbers are loaded into the eax register, added, and the result is stored in the sum variable.

* Floating-Point Operations: The sqrt function in most standard libraries works with floating-point numbers. To use it:

* fild dword [sum]: Loads the integer value from sum onto the Floating-Point Unit (FPU) stack as a double-precision floating-point number.

* fsqrt: Calculates the square root of the top value on the FPU stack. The result remains on the FPU stack.

* fistp dword [sqrt_result]: Stores the integer part of the floating-point result from the FPU stack into the sqrt_result variable. This instruction also pops the value from the FPU stack.

* Output:

* The code displays a result message using the write syscall.

* It then uses the printf function to print the integer part of the calculated square root.

* push dword [sqrt_result]: Pushes the integer result onto the stack as an argument for printf.

* push dword format_int: Pushes the format string "%d" onto the stack.

* call printf: Calls the printf function.

* add esp, 8: Cleans up the stack by removing the arguments passed to printf.

* Finally, it prints a newline character.

* Exit:

* The program exits using the exit syscall (eax = 1).

How to Assemble and Run (Linux x86):

* Save: Save the code as a .asm file (e.g., sqrt_sum.asm).

* Assemble: Use the Netwide Assembler (NASM):

nasm -f elf sqrt_sum.asm -o sqrt_sum.o


* Link: Link the object file with the C standard library:

gcc -m32 sqrt_sum.o -o sqrt_sum -lm


* -m32: Compiles for a 32-bit environment (assuming your assembly is 32-bit).

* -lm: Links with the math library (for sqrt).

* Run: Execute the program:

./sqrt_sum


Important Considerations:

* Error Handling: This code lacks robust error handling (e.g., for non-numeric input).

* Multi-Digit Input: The input conversion is very basic and only works for single-digit numbers. For multi-digit input, you would need to implement a more complex parsing loop.

* Floating-Point Precision: The sqrt function returns a floating-point number. This code converts it to an integer, which truncates any decimal part. If you need more precise results, you would work with floating-point numbers throughout.

* Calling Conventions: This code assumes the standard C calling convention for passing arguments to and returning from external functions.

* Operating System: Syscalls are operating system-specific. This code uses Linux x86 syscalls. For other operating systems, the syscall numbers and calling conventions would be different.

This example provides a basic framework. For a more practical application, you would need to address the limitations mentioned above.

 
 
 

Recent Posts

See All
The chess mistake at iitm

[12/13, 04:56] Dr Harish Exinc: It was missed due to manglik in scientific carer [12/13, 04:58] Dr Harish Exinc: Also due to space space manglik of those showing off their trip to other space spaces o

 
 
 
The most important button

Will game button on phone solve high speed gaming problem instead of all of us just making world famous video after other? Where you can choose 2 games click after double click and it changes as per a

 
 
 

Comments


bottom of page