Lab 5 -- .data section, along with printing, xor, not, and neg

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".

Part 1

You can copy the code from part 2 of lab 4 to a new file, such as "lab5_pt1.asm".

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:

Part 2

Create a new file for this part, such as "lab5_pt2.asm". Using the same integer values, NOT each one and print the result as a hexadecimal value. That is, print "Not result" then use "hexstr" to show the value.

    not eax              ; Perform NOT operation 

Use the same integer values as before, but NOT int2. Add int1 and the NOT int2 value together, and show the result. Do not change the value of int2, that is, do not write the value of NOT int2 back to memory.
Repeat this, but use the NEG command instead, with the appropriate string. We can use "neg" command to negate the value as follows:

    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.

Part 3

Create a new file for this part, such as "lab5_pt3.asm". Use the same int values, but XOR them with all 1's. That is, we will load all 1's in a register and perform its XOR operation with our data. This is how it looks for int1.

    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.

Questions:

Part 4

Here is some code to negate int2, write it back to memory, then add it to int1, print the negated int2, and print the sum. Create a new file for this part, such as "lab5_pt4.asm". Do not call it "lab5d.asm" because that name is used by the code shown below. Make sure to update the comments at the top of the new file to specify the correct compilation and link commands.

    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.
Questions:

What we learned in this lab: