148k views
3 votes
Arithmetic Instructions

Using only registers (no input) add 1 + 2 and display the result. You can move the result to a variable then print it.

User Candyce
by
7.9k points

1 Answer

1 vote

Answer:

Assuming we have a register for holding the result, the following assembly code will add 1 and 2 together and store the result in the register, then print the result to the console:

mov eax, 1 ; move the value 1 into the eax register

add eax, 2 ; add the value 2 to the eax register

mov ebx, eax ; move the value in eax into the ebx register

; At this point, ebx contains the result of 1 + 2

To print the result, we can use system calls. The specific system calls required will depend on the operating system being used. Here is an example using the Linux system call interface:

mov eax, 4 ; system call number for "write" (printing to console)

mov ebx, 1 ; file descriptor for stdout

mov ecx, ebx ; pointer to the string to be printed

mov edx, 1 ; length of the string to be printed (in bytes)

int 0x80 ; call the kernel to perform the write operation

; convert the result to a string and print it

add ebx, 48 ; convert result to ASCII code for printing

mov ecx, ebx ; move result to ecx for printing

mov edx, 1 ; length of the string to be printed (in bytes)

mov eax, 4 ; system call number for "write" (printing to console)

int 0x80 ; call the kernel to perform the write operation

Step-by-step explanation:

This will print the result of 1 + 2 to the console as the character "3".

User Victor Marchuk
by
8.0k points