Question

Suppose we have C program, int foo(long s, long d) {     char arr_a[11];     int...

Suppose we have C program,

int foo(long s, long d) {
    char arr_a[11];
    int i;
    arr_a[10] = 0;
    arr_s[10] = 0;

    for (i = 0; i < 5; i++) {
      if ((s == 1) && (d == 2)) {
   arr_a[i] = hello[4-i];
   arr_s[i] = hello[4-i];
   arr_a[i+5] = world[i];
   arr_s[i+5] = world[i];
      } else {
   arr_a[i+5] = hello[4-i];
   arr_s[i+5] = hello[4-i];
   arr_a[i] = world[i];
   arr_s[i] = world[i];
      }
    }
    printf("Result: %d, %d - %s - %s\n", (int) s, (int) d, arr_a, arr_s);

    bar(s, d);
    return 0;
}

and I convert the code into X86 assembly code and I want to know what does each lines of code doing.

Could you please make simple comments next to each line for me to understand? Thank you.

foo:
.LFB0:
   .cfi_startproc
   pushq   %rbp   #                       
   .cfi_def_cfa_offset 16
   .cfi_offset 6, -16
   movq   %rsp, %rbp   #,                  
   .cfi_def_cfa_register 6
   subq   $32, %rsp   #,                  
   movq   %rdi, -24(%rbp)   # s, s          
   movq   %rsi, -32(%rbp)   # d, d          
   movb   $0, -6(%rbp)   #, arr_a      
   movb   $0, arr_s+10(%rip)   #, arr_s  
   movl   $0, -4(%rbp)   #, i          
   jmp   .L2   #
.L5:
   cmpq   $1, -24(%rbp)   #, s                   Compare immediate value of 1 and value of s
   jne   .L3   #,                                       if s is not equal to 1, jump to L3
   cmpq   $2, -32(%rbp)   #, d                   Compare immediate value of 2 and value of d
   jne   .L3   #,                                       if d is not equal to 2, jump to L3
   movl   $4, %eax   #, tmp119                   move immediate value of 4 into register
   subl   -4(%rbp), %eax   # i, D.2343               allocate space for integer i
   cltq                                           # sign-extends EAX(32) to RAX(64)
   movzbl   hello(%rax), %edx   # hello, D.2344       load hello with zero-extend to full register edx
   movl   -4(%rbp), %eax   # i, tmp122               pass i into register
   cltq                                           # sign-extends EAX(32) to RAX(64)
   movb   %dl, -16(%rbp,%rax)   # D.2344, arr_a       pass hello to arr_a
   movl   $4, %eax   #, tmp123                   move immediate value of 4 into register
   subl   -4(%rbp), %eax   # i, D.2343               allocate space for integer i
   cltq                                           # sign-extends EAX(32) to RAX(64)
   movzbl   hello(%rax), %edx   # hello, D.2344       load hello with zero-extend to full register edx
   movl   -4(%rbp), %eax   # i, tmp126               pass i into register
   cltq                                           # sign-extends EAX(32) to RAX(64)
   movb   %dl, arr_s(%rax)   # D.2344, arr_s       pass hello to arr_s
   movl   -4(%rbp), %eax   # i, tmp127               pass i into register
   leal   5(%rax), %ecx   #, D.2343               pass address of memory into register
   movq   world(%rip), %rdx   # world, D.2345       pass world to register
   movl   -4(%rbp), %eax   # i, tmp128               pass i into register
   cltq                                           # sign-extends EAX(32) to RAX(64)
   addq   %rdx, %rax   # D.2345, D.2345          
   movzbl   (%rax), %edx   # *_19, D.2344          
   movslq   %ecx, %rax   # D.2343, tmp129
   movb   %dl, -16(%rbp,%rax)   # D.2344, arr_a
   movl   -4(%rbp), %eax   # i, tmp130
   leal   5(%rax), %ecx   #, D.2343
   movq   world(%rip), %rdx   # world, D.2345
   movl   -4(%rbp), %eax   # i, tmp131
   cltq                                           # sign-extends EAX(32) to RAX(64)
   addq   %rdx, %rax   # D.2345, D.2345
   movzbl   (%rax), %edx   # *_25, D.2344
   movslq   %ecx, %rax   # D.2343, tmp132
   movb   %dl, arr_s(%rax)   # D.2344, arr_s
   jmp   .L4   #
.L3:
   movl   -4(%rbp), %eax   # i, tmp133
   leal   5(%rax), %ecx   #, D.2343
   movl   $4, %eax   #, tmp134
   subl   -4(%rbp), %eax   # i, D.2343
   cltq                                           # sign-extends EAX(32) to RAX(64)
   movzbl   hello(%rax), %edx   # hello, D.2344
   movslq   %ecx, %rax   # D.2343, tmp136
   movb   %dl, -16(%rbp,%rax)   # D.2344, arr_a
   movl   -4(%rbp), %eax   # i, tmp137
   leal   5(%rax), %ecx   #, D.2343
   movl   $4, %eax   #, tmp138
   subl   -4(%rbp), %eax   # i, D.2343
   cltq
   movzbl   hello(%rax), %edx   # hello, D.2344
   movslq   %ecx, %rax   # D.2343, tmp140
   movb   %dl, arr_s(%rax)   # D.2344, arr_s
   movq   world(%rip), %rdx   # world, D.2345
   movl   -4(%rbp), %eax   # i, tmp141
   cltq
   addq   %rdx, %rax   # D.2345, D.2345
   movzbl   (%rax), %edx   # *_38, D.2344
   movl   -4(%rbp), %eax   # i, tmp143
   cltq
   movb   %dl, -16(%rbp,%rax)   # D.2344, arr_a
   movq   world(%rip), %rdx   # world, D.2345
   movl   -4(%rbp), %eax   # i, tmp144
   cltq
   addq   %rdx, %rax   # D.2345, D.2345
   movzbl   (%rax), %edx   # *_43, D.2344
   movl   -4(%rbp), %eax   # i, tmp146
   cltq
   movb   %dl, arr_s(%rax)   # D.2344, arr_s
.L4:
   addl   $1, -4(%rbp)   #, i
.L2:
   cmpl   $4, -4(%rbp)   #, i
   jle   .L5   #,
   movq   -32(%rbp), %rax   # d, tmp147
   movl   %eax, %edx   # tmp147, D.2343
   movq   -24(%rbp), %rax   # s, tmp148
   movl   %eax, %esi   # tmp148, D.2343
   leaq   -16(%rbp), %rax   #, tmp149
   movl   $arr_s, %r8d   #,
   movq   %rax, %rcx   # tmp149,
   movl   $.LC1, %edi   #,
   movl   $0, %eax   #,
   call   printf   #
   movq   -32(%rbp), %rdx   # d, tmp150
   movq   -24(%rbp), %rax   # s, tmp151
   movq   %rdx, %rsi   # tmp150,
   movq   %rax, %rdi   # tmp151,
   call   bar   #
   movl   $0, %eax   #, D.2343
   leave
   .cfi_def_cfa 7, 8
   ret
   .cfi_endproc
.LFE0:
   .size   foo, .-foo
   .globl   main
   .type   main, @function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assuming you are using x86 processor and GAS(GNU Assembler) as you have not mentioned which assembler and processor you are using.

Here is the meaning of each line:

foo: Calling function foo. .LFB0: .LF<something>: is a label that probably referenced by exception-related data. .cfi_startproc cfi_startproc is a directive & is used at the beginning of each function. It initializes internal data structures. pushq %rbp # push the value of %rbp into the stack. 'q' is just the suffix and is stands for quad (64 bit). '%rbp' is a base pointer which points base of current stack .cfi_def_cfa_offset 16 cfi_def_cfa_offset is a directive which is inserted to indicate that cfa is at offset of 16 in the stack from the stack pointer .cfi_offset 6, -16 cfi_offset tells assembler that the previous value of register (in the case 6) is saved at offset(i.e -16) offset from CFA. movq %rsp, %rbp #, movq is used to move a quadword of 64-bit from source(%rsp) to destination(%rbp). Both the '%rsp' and '%rbp' are the special purpose registers where '%rsp' is stack pointer and '%rbp' is base pointer .cfi_def_cfa_register 6 .cfi_def_cfa_register modifies a rule for computing CFA. And 6 is the register number. subq $32, %rsp #, Allocates the stack space where value of $32 will be the size of the stack frame movq %rdi, -24(%rbp) # s, s movq moves the value of %rdi to the 24 spaces away from the base pointer(%rbp) movq %rsi, -32(%rbp) # d, d movq moves the value of %rsi to the 32 spaces away from the base pointer(%rbp) movb $0, -6(%rbp) #, arr_a movb moves the value of $0 to the 6 spaces away from the base pointer(%rbp) where 'b' is the suffix and stands for byte(8 bit) movb $0, arr_s+10(%rip) #, arr_s movb moves the value of $0 to the value of arr_s+10(if the value of arr_s is 2 then 2+10=12) spaces away from the base pointer(%rbp) where 'b' is the suffix and stands for byte(8 bit) movl $0, -4(%rbp) #, i movl moves the value of $0 to the 4 spaces away from the base pointer(%rbp) jmp .L2 # jump to label L2 .L5: Label L5 cmpq $1, -24(%rbp) #, s Compare immediate value of 1 and value of s jne .L3 #, if s is not equal to 1, jump to L3 cmpq $2, -32(%rbp) #, d Compare immediate value of 2 and value of d jne .L3 #, if d is not equal to 2, jump to L3 movl $4, %eax #, tmp119 move immediate value of 4 into register subl -4(%rbp), %eax # i, D.2343 allocate space for integer i cltq # sign-extends EAX(32) to RAX(64) movzbl hello(%rax), %edx # hello, D.2344 load hello with zero-extend to full register edx movl -4(%rbp), %eax # i, tmp122 pass i into register cltq # sign-extends EAX(32) to RAX(64) movb %dl, -16(%rbp,%rax) # D.2344, arr_a pass hello to arr_a movl $4, %eax #, tmp123 move immediate value of 4 into register subl -4(%rbp), %eax # i, D.2343 allocate space for integer i cltq # sign-extends EAX(32) to RAX(64) movzbl hello(%rax), %edx # hello, D.2344 load hello with zero-extend to full register edx movl -4(%rbp), %eax # i, tmp126 pass i into register cltq # sign-extends EAX(32) to RAX(64) movb %dl, arr_s(%rax) # D.2344, arr_s pass hello to arr_s movl -4(%rbp), %eax # i, tmp127 pass i into register leal 5(%rax), %ecx #, D.2343 pass address of memory into register movq world(%rip), %rdx # world, D.2345 pass world to register movl -4(%rbp), %eax # i, tmp128 pass i into register cltq # sign-extends EAX(32) to RAX(64) addq %rdx, %rax # D.2345, D.2345 Deallocate stack space movzbl (%rax), %edx # *_19, D.2344 movzbl is the instruction and it fetches the byte stored at the sum of both the parameters movslq %ecx, %rax # D.2343, tmp129 movslq is mov and sign-extend a value from source of 32-bit to destination of 64-bit movb %dl, -16(%rbp,%rax) # D.2344, arr_a movb moves the value of %dl to the 24 spaces away from the %rbp and %rax where in '%dl' l is low byte. movl -4(%rbp), %eax # i, tmp130 movl moves long(32-bit) from source(value at 4 bits away from %rbp) to destination(%eax). '%eax is 32-bit general purpose register. leal 5(%rax), %ecx #, D.2343 leal stands for load effective address and it computes the final memory address according to addressing mode and stores it in a register movq world(%rip), %rdx # world, D.2345 pass world to register movl -4(%rbp), %eax # i, tmp131 movl moves the value positioned 4 space away from %rbp to %eax cltq # sign-extends EAX(32) to RAX(64) addq %rdx, %rax # D.2345, D.2345 Deallocate the stack space movzbl (%rax), %edx # *_25, D.2344 movzbl is the instruction and it fetches the byte stored at the sum of both the parameters movslq %ecx, %rax # D.2343, tmp132 movslq is mov and sign-extend a value from source of 32-bit to destination of 64-bit movb %dl, arr_s(%rax) # D.2344, arr_s mov value of %dl to %rax jmp .L4 # jump tp label L4 .L3: Label L3 movl -4(%rbp), %eax # i, tmp133 pass i into register leal 5(%rax), %ecx #, D.2343 pass memory address into register movl $4, %eax #, tmp134 move immediate value of 4 into register subl -4(%rbp), %eax # i, D.2343 allocate space for integer i cltq # sign-extends EAX(32) to RAX(64) movzbl hello(%rax), %edx # hello, D.2344 load hello with zero-extend to full register edx movslq %ecx, %rax # D.2343, tmp136 movb %dl, -16(%rbp,%rax) # D.2344, arr_a pass hello to arr_a movl -4(%rbp), %eax # i, tmp137 pass i into register leal 5(%rax), %ecx #, D.2343 pass memory address into register movl $4, %eax #, tmp138 move immediate value of 4 into register subl -4(%rbp), %eax # i, D.2343 allocate space for i cltq # sign-extends EAX(32) to RAX(64) movzbl hello(%rax), %edx # hello, D.2344 load hello with zero-extend to full register edx movslq %ecx, %rax # D.2343, tmp140 movb %dl, arr_s(%rax) # D.2344, arr_s pass hello to arr_s movq world(%rip), %rdx # world, D.2345 pass world to register movl -4(%rbp), %eax # i, tmp141 pass i to register cltq # sign-extends EAX(32) to RAX(64) addq %rdx, %rax # D.2345, D.2345 Deallocate stack space movzbl (%rax), %edx # *_38, D.2344 load hello with zero-extend to full register edx movl -4(%rbp), %eax # i, tmp143 allocate space for i cltq # sign-extends EAX(32) to RAX(64) movb %dl, -16(%rbp,%rax) # D.2344, arr_a pass hello to arr_a movq world(%rip), %rdx # world, D.2345 pass world to register movl -4(%rbp), %eax # i, tmp144 pass i to register cltq # sign-extends EAX(32) to RAX(64) addq %rdx, %rax # D.2345, D.2345 Deallocate stack space movzbl (%rax), %edx # *_43, D.2344 load hello with zero-extend to full register edx movl -4(%rbp), %eax # i, tmp146 space allocation for i cltq # sign-extends EAX(32) to RAX(64) movb %dl, arr_s(%rax) # D.2344, arr_s pass value of $dl to arr_s .L4: Label L4 addl $1, -4(%rbp) #, i Deallocate space of i .L2: Label L2 cmpl $4, -4(%rbp) #, i Compare immediate value of 4 and value of i jle .L5 #, if i is less than equal to 4 jump to L5 movq -32(%rbp), %rax # d, tmp147 pass value of %rbp to register movl %eax, %edx # tmp147, D.2343 pass value to register movq -24(%rbp), %rax # s, tmp148 pass s to register movl %eax, %esi # tmp148, D.2343 pass value to register leaq -16(%rbp), %rax #, tmp149 movl $arr_s, %r8d #, pass value of arr_s to register movq %rax, %rcx # tmp149, pass value to register movl $.LC1, %edi #, pass value of $.Lc1 to register movl $0, %eax #, move immediate value of 0 into register call printf # calling the printf function movq -32(%rbp), %rdx # d, tmp150 pass value of %rbp to register movq -24(%rbp), %rax # s, tmp151 pass s to register movq %rdx, %rsi # tmp150, move value of %rdx to %rsi movq %rax, %rdi # tmp151, move value of %rax to %rdi call bar # calling bar function movl $0, %eax #, D.2343 move immediate value of 0 into register leave Leave the stack frame .cfi_def_cfa 7, 8 Take address from 7 and add offset 8 to it ret transfer control to return address on stack .cfi_endproc Close the function .LFE0: .LF<something>: is a label that probably referenced by exception-related data. .size foo, .-foo .globl main Declare main as global variable .type main, @function Declare main as function

Tried the best of me.

Thank you.

Add a comment
Know the answer?
Add Answer to:
Suppose we have C program, int foo(long s, long d) {     char arr_a[11];     int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You know the following assembly code snippet is from a recursive function in C. You also...

    You know the following assembly code snippet is from a recursive function in C. You also know that the stack contents at a particular point in time when we are in the recursive function are shown on the next page. Answer the following questions: a) how many Foo stack frames are on the stack? b) what is the return address back to the function that called Foo for the first time? c) what is the return address back into the...

  • Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from...

    Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from github because it won't be the right answer. .file "mystery.c" .comm num,1600,32 .text .globl add .type add, @function add: .LFB2: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movq %rdi, -8(%rbp) movq %rsi, -16(%rbp) movq -16(%rbp), %rax movq -8(%rbp), %rdx addq %rdx, %rax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE2: .size add, .-add .globl dothething .type dothething, @function...

  • Consider the following source code, where b, c, and d are constants declared with #define. You...

    Consider the following source code, where b, c, and d are constants declared with #define. You will need to determine the values for b, c, and d. struct lnode { char *str; struct lnode *next; } struct lnode A[b][c][d]; int store_ele(int h, int i, int j, struct lnode dest) { A[h][i][j] = dest; return sizeof(A);} On compiling this program (with -O2), GCC generates the following assembly code for the store_ele function: store_ele: movslq %edi, %rdi movslq %esi, %rsi movslq %edx,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT