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 class (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


Notice how there is some repetition: a set of about 10 lines is repeated 3 times, though the last repetition is a bit shorter. Where are the repetitions? What are the differences between them?

In the next step, we will compile the "lab2_example.s" program. Remember that "gcc" is the GNU C compiler, which also works as an assembler. Use "gcc" to create an object file called "hello.o". An object file is created before linking, and linking produces the executable file. Notice the "-c" argument that is passed to "gcc". What does the "-c" argument to gcc mean? Where did you find this answer?

[mweeks@gsuad.gsu.edu@snowball ~]$ gcc -c lab2_example.s

After compiling, get a list of files with the "ls -l" command. The "-l" option says that we want a long list, i.e. showing details. Notice how the "ls" output shows the new file "lab2_example.o".

For the next step, we will use the object file to create an executable file. Again, we use "gcc" for this. Notice the "-o" argument that is passed to "gcc". The arguments "-o lab2_example" specify that we want the output to be a new file called "lab2_example". Be careful with this step; if you were to type "-o lab2_example.s", it would over-write the S file, and you would have to start over. The output file that this command creates is called "lab2_example1".

[mweeks@gsuad.gsu.edu@snowball ~]$ gcc lab2_example.o -o lab2_example1

You might be wondering if we must compile and link the program separately. In fact, we can put them together, as in the following.

[mweeks@gsuad.gsu.edu@snowball temp]$ gcc lab2_example.s -o lab2_example2

Now run the executable program.

[mweeks@gsuad.gsu.edu@snowball temp]$ ./lab2_example1
4 0
14 15
-7 -13


Also run "lab2_example2". Do you get the same results? Why or why not?

Now let's figure out what is going on. The movl command is used to move a piece of "long" data (32 bits) from one place to another, zeroing out the upper 32 bits of the destination register. The base pointer holds the address for the frame pointer, and can be used for local variables. See pages 290--296 of your textbook (7th ed.) for more information. That is, a local variable may be at (%rbp - 4), and another local variable at (%rbp - 8), etc. For example,

        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:




Remember to remove any control characters that might be generated from arrow keys, using control-(some key), backspace, etc., in the file that you upload to iCollege. In other words, what you turn in should look like what the text on your screen looks like. Upload the log file to iCollege, making sure to use a .txt extension. Do not upload screen-shots.