NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: puttyios on June 16, 2012, 12:55:19 PM

Title: question about sddressing on stack
Post by: puttyios 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?
Title: Re: question about sddressing on stack
Post by: Frank Kotler 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

Title: Re: question about sddressing on stack
Post by: puttyios on June 18, 2012, 12:10:44 PM
is this 'x' 'w' defined as global  variable or  local ?
Title: Re: question about sddressing on stack
Post by: Frank Kotler 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

Title: Re: question about sddressing on stack
Post by: puttyios on July 01, 2012, 11:46:10 AM
thank you Frank!

I got that!

Best Regard!