Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assembly "mov" instruction

I'm learning assembly by comparing a c program to its assembly equivalent.

Here is the code.

.file   "ex3.c"
.section    .rodata
.LC0:
    .string "I am %d years old.\n"
.LC1:
    .string "I am %d inches tall.\n"
    .text
    .globl  main
    .type   main, @function
main:
    pushl   %ebp    //establish stack frame//
    movl    %esp, %ebp //move esp into ebp, all contents saved down stack//
    andl    $-16, %esp //16 from esp for local var space//
    subl    $32, %esp//stack frame reserving - 32 bytes//
    movl    $10, 24(%esp)
    movl    $72, 28(%esp)
    movl    24(%esp), %eax
    movl    %eax, 4(%esp)
    movl    $.LC0, (%esp)
    call    printf
    movl    28(%esp), %eax
    movl    %eax, 4(%esp)
    movl    $.LC1, (%esp)
    call    printf
    movl    $0, %eax
    leave
    ret
    .size   main, .-main
    .ident  "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
    .section    .note.GNU-stack,"",@progbits

For this line:

movl    $10, 24(%esp)

If I understand it correctly it is saying move the value of 10 into the esp register. But what is the 24 doing? I don't think it is moved into esp because a value to be moved is denoted by "$" (i think)

like image 624
user2263800 Avatar asked Jul 14 '14 21:07

user2263800


2 Answers

movl $10,24(%esp)

means: move a literal decimal-10 long (4-bytes) into a 4-byte memory location that begins at the address pointed to by (the esp register plus decimal 24)--basically it is a local variable.

like image 168
Dwayne Towell Avatar answered Oct 20 '22 13:10

Dwayne Towell


In other words movl $10,24(%esp)

means: load 10 into *(esp + 24)

In C that equals to:

*(unsigned long *)(myptr + 24) = 10;

where myptr is taken with value of esp register.

like image 43
Ruslan Gerasimov Avatar answered Oct 20 '22 14:10

Ruslan Gerasimov