Before we begin Lab 5, refer to Lab 4 to get a quick reminder on commands like mov, add, call. You should also have a clear understanding of how we print the values in NASM by calling "printf".
First, load integer values from the data section into two registers, add them together, and store the result in another location in the data section. Then, print the text "Add result " followed by the result. Here is an example of how the data section might look.
section .data ; Data section, initialized variables
str1: db "Add result ", 0 ; String to use
str2: db "Sub result ", 0 ; String to use
str3: db "Not result ", 0 ; String to use
str4: db "Neg result ", 0 ; String to use
decstr: db "%d", 10, 0 ; String format to use (decimal), followed by NL
hexstr: db "%x", 10, 0 ; String format to use (hex), followed by NL
int1: dd 12
int2: dd 5
addresult: dd 0
subresult: dd 0
By the way, "db", "dd", and similar keywords are used in a similar
way as a variable declaration. Value(s) of data defined as bytes are "db"
while value(s) of data as defined as doubles
(in the specific sense of x86 literature)
are "dd".
Once you have the addition working, load the integer values into two registers,
perform subtraction, print the text "Sub result " followed by the result.
The addition and subtraction code should go into the same .asm file.
Questions:
not eax ; Perform NOT operation
mov eax, [int2]
neg eax ; Perform negation
add eax, [int1]
Questions:
Refer to figure 2.1 from your book (the basic diagram of a
microcomputer, or page 4 of the chapter 2 slides) in your answers
for the first two.
mov eax, [int1]
mov ebx, 0xFFFFFFFF ; Bitmask with all bits set to 1
xor eax, ebx ; XOR with the bitmask
Print the result of XOR out. Also print the result of NOT.
extern printf ; We will use this external function
section .data ; Data section, initialized variables
mystr: db "%d", 10, 0 ; String format to use (decimal), followed by NL
int1: db 10
int2: db 5
sum: dq 0
section .text
global main
main:
mov ax, [int2]
neg ax
mov [int2], ax
add eax, [int1]
mov [sum], eax
; Now print the result out
mov rdi, mystr ; Format of the string to print
mov rsi, [int2] ; Value to print
mov rax, 0
call printf
; Now print the result out
mov rdi, mystr ; Format of the string to print
mov rsi, [sum] ; Value to print
mov rax, 0
call printf
mov rax, 0
ret
Assemble it, and run it. It does not give the output that we expect.
What we learned in this lab: