Skip to content

Stackpointer subtraction in listings 14-4, 14-5, 14-6 #28

@yeah-boi

Description

@yeah-boi

The assembly code for listing 14-4 in listing 14-5 seems a bit wierd. RSP is not subtracted enough (only 0xf90) to hold char buffer[1024]. buffer is partly outside the stack and both a and b are completly outside the stack.

...
4004ba:       48 81 ec 90 0f 00 00    sub    rsp,0xf90
4004c1:       89 bd fc ef ff ff       mov    DWORD PTR [rbp-0x1004],edi
4004c7:       89 b5 f8 ef ff ff       mov    DWORD PTR 
...

Same issue with 14-6.

When I compiled and disassembled myself I got some extra bloat at 40055d-40056a and 400588-400597 (maybe you know why?), but RSP is subtracted more than enough (x1020) to hold buffeer, a and b:

$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc main.c -o test

$objdump -d -M intel test

...
0000000000400546 <maximum>:
  400546:	55                   	push   rbp
  400547:	48 89 e5             	mov    rbp,rsp
  40054a:	48 81 ec 20 10 00 00 	sub    rsp,0x1020
  400551:	89 bd ec ef ff ff    	mov    DWORD PTR [rbp-0x1014],edi
  400557:	89 b5 e8 ef ff ff    	mov    DWORD PTR [rbp-0x1018],esi
  40055d:	64 48 8b 04 25 28 00 	mov    rax,QWORD PTR fs:0x28
  400564:	00 00 
  400566:	48 89 45 f8          	mov    QWORD PTR [rbp-0x8],rax
  40056a:	31 c0                	xor    eax,eax
  40056c:	8b 85 ec ef ff ff    	mov    eax,DWORD PTR [rbp-0x1014]
  400572:	3b 85 e8 ef ff ff    	cmp    eax,DWORD PTR [rbp-0x1018]
  400578:	7d 08                	jge    400582 <maximum+0x3c>
  40057a:	8b 85 e8 ef ff ff    	mov    eax,DWORD PTR [rbp-0x1018]
  400580:	eb 06                	jmp    400588 <maximum+0x42>
  400582:	8b 85 ec ef ff ff    	mov    eax,DWORD PTR [rbp-0x1014]
  400588:	48 8b 55 f8          	mov    rdx,QWORD PTR [rbp-0x8]
  40058c:	64 48 33 14 25 28 00 	xor    rdx,QWORD PTR fs:0x28
  400593:	00 00 
  400595:	74 05                	je     40059c <maximum+0x56>
  400597:	e8 84 fe ff ff       	call   400420 <__stack_chk_fail@plt>
  40059c:	c9                   	leave  
  40059d:	c3                   	ret    
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions