Question: PART A: We will look at C code versus machine code. Write the following code in cygwin using vi and save it as code.c. int

PART A:

We will look at C code versus machine code. Write the following code in cygwin using vi and save it as code.c.

int main()

{

int i=1;

int j=1;

j=i+10;

j++;

return 0;

}

Compile the code with the following format.

$> gcc S code.c

This will create an assembly file code.s.

Use the cat command to view the contents of code.s.

The assembly code will look something like the following. The annotations will help explain what the code does.

movl $1, -4(%rbp) //Move first variable onto stack (rbp is base pointer), set value to 1

movl $1, -8(%rbp) //Move second variable onto stack, set value to 1

movl -4(%rbp), %eax //Move first variable to register %eax

addl $10, %eax //Add 10 to variable in register %eax

movl %eax, -8(%rbp) //Move the value in register %eax to the second variable

addl $1, -8(%rbp) //Add 1 to the second variable

movl $0, %eax //Set the value of register %eax to 0

addq $48, %rsp //Reset stack pointer

popq %rbp //Restore frame pointer

ret

Using the above as an example, write C code to yield the following assembly code. Note that %eax and %edx are two different registers.

movl $0, -4(%rbp)

movl $0, -8(%rbp)

movl $0, -12(%rbp)

movl -8(%rbp), %eax

movl -4(%rbp), %edx

addl %edx, %eax

movl %eax, -12(%rbp)

movl $0, %eax

addq $48, %rsp

popq %rbp

ret

Write C code below.

Part B:

Linux and Windows show assembly in different formats. The code differences are shown below.

movl 8(%ebp), %edx //Linux move the value in base pointer to edx register

movl edx, DWORD PTR [ebp+8]

Copy the code below into a new Visual Studio Project. Compile the C++ code. In the Solution Explorer, right click on the .cpp file and choose properties.

Pick C/C++, then output files in the assembler output section, choose assembly, machine code, and source. This will produce a .cod file in your ProjectName\Debug folder.

The output shows the assembly language code with the original source code interleaved.

Here is an example. x = 3 ;

0041163E mov dword ptr [x],3

address instruction destination, source // opposite order from Linux dword ptr [x] represents the address in memory of the variable x.

C++ Code:

#include

#include

#include

int main()

{

int x = rand() ; // initialization uses random number to prevent

// the optimizer from doing all the compuations at

// compile time

int y = rand() ;

int z = rand() ;

/* explain code starting here */

int total = x * ( y - z ) * 2 ;

printf ( "%d ", total ) ; // this is needed to prevent the

// optimzier from generating 0 lines

// of code

if ( x < y )

z *= 4 ;

else

x *= 3 ;

int array[3] ;

array[0] = x ;

array[1] = y ;

array[2] = z ;

for ( int i = 0 ; i < 3 ; i++ )

array[i] *= 50 ;

/* explain code ending here */

printf( "%d %d %d ", array[0], array[1], array[2] ) ;

}

Run the above program in debug mode. In your ProjectName\Debug folder you will find a .cod file. Open this file with Visual Studio. Cut and paste all of the assembly language code below. For each block of assembly language, annotate with a description of what the instruction is doing. You can step through the program and watch the changes to the registers and to the local variables in order to give you a feel for what the assembly language code is doing. Dont forget to submit this lab. Use the naming convention GSP215_LastnameFi_ Week3_Lab.zip, where you replace Lastname with your last name and Fi with your first initial.

Paste cod file with annotations here

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!