Author Topic: question about sddressing on stack  (Read 12042 times)

Offline puttyios

  • Jr. Member
  • *
  • Posts: 26
question about sddressing on stack
« on: June 16, 2012, 12:55:19 PM »
I have the c code here:
#include <stdio.h>
9
10 int main(void)
11 {
12 int w, x, y, z;
13
14 printf("Enter two integers: ");
15 scanf("%i %i", &w, &x);
16 y = w + x;
17 z = w - x;
18 printf("sum = %i, difference = %i\n", y, z);
19
20 return 0;
21 }


and   I  write  the  assembly code , a part is below :

38 movl w(%rbp), %esi # y = w

39 addl x(%rbp), %esi # y += x

45 movl w(%rbp), %edx # z = w

46 subl x(%rbp), %edx # z -= x

my question is  :  "w(%rbp)"  is corresponding to  the 'w' local  variable?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: question about sddressing on stack
« Reply #1 on: June 16, 2012, 05:20:24 PM »
Yeah, I guess. I ASSume "w" and "x" are defined somewhere? Possibly something like:
Code: [Select]
.equ w -8
.equ x -16
or some such... I'm not sure about 64-bit code... might be some alignment padding added(?). What happens if the pesky user enters "xyz"?

Best,
Frank


Offline puttyios

  • Jr. Member
  • *
  • Posts: 26
Re: question about sddressing on stack
« Reply #2 on: June 18, 2012, 12:10:44 PM »
is this 'x' 'w' defined as global  variable or  local ?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: question about sddressing on stack
« Reply #3 on: June 18, 2012, 12:31:11 PM »
'x' and 'w' are just numbers (constants, not variables). They're used as offsets from rbp to locate "local" ("stack" or "automatic") variables.

Best,
Frank


Offline puttyios

  • Jr. Member
  • *
  • Posts: 26
Re: question about sddressing on stack
« Reply #4 on: July 01, 2012, 11:46:10 AM »
thank you Frank!

I got that!

Best Regard!