Lab 2 -- mov, and, or, not, along with local C variables
First, get the "lab2_example.s" file. You can download it from here. You will need to put this under your account on SNOWBALL. You can use sftp as shown in the lab (remember that there is an example here). There are many other ways to do this, too, and sftp is not required for this assignment.
There are questions to answer throughout this lab. You can write the answers to a text file, and append it to your lab. Or you can write the answers to a text file, and "cat" it to the log. For example, you can use the vi editor to create your response, followed by cat lab2notes.txt (or whatever you call the file if it is not "lab2notes.txt"). For each answer, make sure to include the question text.
Now, log in to SNOWBALL (e.g. "ssh your_account@snowball.cs.gsu.edu").
Create a log file with the "script" command.
Next, use the "cat" command to display the contents of
the "lab2_example.s" file. You should see content like the following.
.file "lab2_example.c"
.section .rodata
.LC0:
.string "%d %d\n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl $6, -4(%rbp)
movl $12, -8(%rbp)
movl $3, -12(%rbp)
movl -12(%rbp), %eax
movl -8(%rbp), %edx
andl %eax, %edx
movl -8(%rbp), %eax
movl -4(%rbp), %ecx
andl %ecx, %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl -12(%rbp), %eax
movl -8(%rbp), %edx
orl %eax, %edx
movl -8(%rbp), %eax
movl -4(%rbp), %ecx
orl %ecx, %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl -8(%rbp), %eax
notl %eax
movl %eax, %edx
movl -4(%rbp), %eax
notl %eax
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
.section .note.GNU-stack,"",@progbits
movl -8(%rbp), %eax
says to move the value from the base pointer, minus 8, to the EAX register,
while
movl -4(%rbp), %ecx
says to move the value from the base pointer, minus 4, to the ECX register,
and
movl $6, -4(%rbp)
moves the "immediate" number 6 to the base pointer - 4.
The base pointer indicates a local memory area, and subtracting 4 or 8
from it allows us to specify a location to hold a 4 byte value.
How many bits are in 4 bytes?
Look up the commands andl, orl, notl, and describe what they do.
There are three variables in the program, with value of 3, 6, and 12. The "lab2_example.s" assembly language program comes from a program originally written in C. In the C compiler that generated this .s file, floats and ints both occupy 4 bytes of memory. From the context of this assembly language listing, what variable type(s) do the original variables have? Explain. Can you tell what the variables were named in the higher level language code? Why or why not? What operations are being performed on these variables to give the results that are shown? Explain.
What if we copy the executable code from one machine to another?
This example was downloaded using sftp, onto another x86 based machine.
When attempting to run it, the following error occurs:
mweeks@air:csc3210_temp$ ./lab2_example
-bash: ./lab2_example: cannot execute binary file
Why do you think is?
Finally, exit from the script command.
This informs the "script" utility that we are done.
[mweeks@gsuad.gsu.edu@snowball
~]$ exit
exit
Script done, file is lab2.log
Next, exit again to log out of SNOWBALL.
[mweeks@gsuad.gsu.edu@snowball
~]$ exit
logout
Connection
to snowball.cs.gsu.edu closed.
You can use "sftp" to get your log file from the server.
In this lab, we have: