Translate x86 intel assembley language TO EQUIVALENT C CODE . Draw a picture wit
ID: 3802280 • Letter: T
Question
Translate x86 intel assembley language TO EQUIVALENT C CODE. Draw a picture with the tables of eax, ebx, ecx, ... and ebp +12, ebp+8, ... showing all the variables
1. (15 points) Write the equivalent C code for the following assembly snippet Hint: all functions in this code take one integer input argument and output one integer result. Show your work next to the assembly statements. No work. No points. globl bar type bar ofunction bar pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax addl. $10, %eax popl %ebp ret globl foo type foo Ofunction. foo pushl %ebp movl %esp, %ebp subl $24, %esp movl %ebx -8 (%ebp) movl %esi. 4 (%ebp) movl 8(%ebp), %ebx movl $1, %eax cmpl $1, %ebx jle L5 movl %ebx, (%esp) call bar movl %eax, %esi subl $1, %ebx movl %ebx, (%esp) call foo imull %esi, %eax L5 movl -8 (%ebp), %ebx movl 4(%ebp), %esi movl %ebp, sp popl %ebp retExplanation / Answer
%ebp is a frame pointer
%esp is a stack pointer
%esp points at return address
Now in this scenario,
.globl bar
.type bar, @function
bar:
pushl %ebp //ebp holds the argument value of caller function
movl %esp, %ebp //ebp=esp;
movl 8(%ebp), %eax //eax= *((int32_t*)(ebp+8));
addl $10, %eax //eax=eax+10;
popl %ebp //returns the value
ret //return to caller function
.globl foo
.type foo, @function
foo:
pushl %ebp //ebp hold the argument value of caller function
movl %esp,%ebp //ebp=esp;
subl $24, %esp //esp=esp-24;
movl %ebx, -8(%ebp) // (u_int32_t)ebp= %ebx;
movl %esi, -4(%ebp) // (u_int32_t)ebp= %esi;
movl 8(%ebp),%ebx //ebx= *((int32_t)(ebp+8));
movl $1, %eax //eax=1;
cmpl $1, %ebx //if(ebx==1) then
jle .L5 //jump to L5
movl %ebx, (%esp) //esp=ebx;
call bar //call the function bar with argument value esp, bar(esp);
movl %eax, %esi //esi=eax;
subl $1, %ebx //ebx=ebx-1;
movl %ebx, (%esp) //esp=ebx;
call foo //call the function foo with argument value esp, foo(asp);
imull %esi, %eax //esi= esi*eax;
.L5:
movl -8(%ebo), %ebx //ebx= *((int32_t*)(ebo-8));
movl -4(%ebp), %esi //ebx= *((int32_t*)(esi-4));
movl %ebp, %esp //esp=ebp;
popl %ebp //retrun result ebp value to the caller function
ret
In this way the program will be compiled. Hence the solution provided.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.