lolly
lolly

Reputation: 25

Having trouble determining the return value in assembly language

I am new to the world of assembly language/code. I find it difficult to understand but I try my best. I'm just having trouble with some of the questions I need to answer for an assignment.

  1. I need to find out the return value of a specific program, if I pass echo $? as a command in the terminal, I get 10 as an answer. However my teacher's answer sheet says the answer is 9.

  2. I need to find out the return value of the main (of the same program) by examining a dump of the assembler code. Again, I think the answer is 10, because 0xa = 10. However, the answer sheet again says the answer is 9. Do I need to subtract a 1 from the return value by default or something? If so, why?

    0x000000000000069f <+0>:     55               push    %rbp
    0x00000000000006a0 <+1>:     48 89 e5         mov    %rsp,%rbp
    0x00000000000006a3 <+4>:     bf 05 00 00 00   mov    $0x5,%edi
    0x00000000000006a8 <+9>:     e8 9d ff ff ff   callq  0x64a <okfisbup>
    0x00000000000006ad <+14>:    b8 0a 00 00 00   mov    $0xa,%eax
    0x00000000000006b2 <+19>:    5d               pop   %rbp
    0x00000000000006b3 <+20>:    c3               retq   
    

When I run the program, by using ./(filename), this is what I get.

 iteratie: 1, buffer address  : 0x7ffe4052d0e0
 iteratie: 2, buffer address  : 0x7ffe4052d1c0
 iteratie: 3, buffer address  : 0x7ffe4052d2a0
 iteratie: 4, buffer address  : 0x7ffe4052d380
 iteratie: 5, buffer address  : 0x7ffe4052d460

And this is the dump of okfisbup:

   0x000000000000064a <+0>:     push   %rbp
   0x000000000000064b <+1>:     mov    %rsp,%rbp
   0x000000000000064e <+4>:     sub    $0xd0,%rsp
   0x0000000000000655 <+11>:    mov    %edi,%eax
   0x0000000000000657 <+13>:    mov    %al,-0xc4(%rbp)
   0x000000000000065d <+19>:    movzbl -0xc4(%rbp),%eax
   0x0000000000000664 <+26>:    sub    $0x1,%eax
   0x0000000000000667 <+29>:    mov    %al,-0x1(%rbp)
   0x000000000000066a <+32>:    cmpb   $0x0,-0x1(%rbp)
   0x000000000000066e <+36>:    je     0x67b <okfisbup+49>
   0x0000000000000670 <+38>:    movzbl -0x1(%rbp),%eax
   0x0000000000000674 <+42>:    mov    %eax,%edi
   0x0000000000000676 <+44>:    callq  0x64a <okfisbup>
   0x000000000000067b <+49>:    movzbl -0xc4(%rbp),%eax
   0x0000000000000682 <+56>:    lea    -0xc0(%rbp),%rdx
   0x0000000000000689 <+63>:    mov    %eax,%esi
   0x000000000000068b <+65>:    lea    0xb6(%rip),%rdi        # 0x748
   0x0000000000000692 <+72>:    mov    $0x0,%eax
   0x0000000000000697 <+77>:    callq  0x520 <printf@plt>
   0x000000000000069c <+82>:    nop
   0x000000000000069d <+83>:    leaveq 
   0x000000000000069e <+84>:    retq

Upvotes: 2

Views: 472

Answers (2)

Joshua
Joshua

Reputation: 43188

    movq $0xa, %eax
    popq rbp
    retq

The return value is 10. The answer sheet is wrong.

The hypothesis that there's a hidden call to exit is unfair without the code given, and testing with $? revealed 10. That's enough.

EDIT: Disassembly of okfisbup has now been posted. It only calls iself and printf.

Upvotes: 2

0___________
0___________

Reputation: 67476

The return value is 10 and it your teacher is wrong

https://godbolt.org/z/8YsnvPahM

int main(void)
{
    return 10;
}

main:
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $10, %eax
        popq    %rbp
        ret

Upvotes: 1

Related Questions