jceceniceros
jceceniceros

Reputation: 87

Call atoi (Assembler) printf (Assembler)

I'm practicing programming in Assembly, making code C. I don't understand the conversion of a parameter to an integer using atoi. Can someone explain to me how I as interpret the following code segment:

movl    12(%ebp), %eax  ; move a parameter to %eax
addl    $4, %eax        ; add 4 bytes
movl    (%eax), %eax    ; magic things
movl    %eax, (%esp)    ; magic things
call    atoi            ; atoi call
movl    %eax, 24(%esp)  ; asign the result of a magic procedure to a new variable

I understand some instructions, but the magic procedures are a little bit ambiguous to me.

Also, I want to know how works the call to the function printf, this is the segment of the code:

movl    $.LC1, %eax  ; assing the string (%d\n) to %eax
movl    28(%esp), %edx  ; move the value to print to %edx
movl    %edx, 4(%esp)   ; magic things
movl    %eax, (%esp)    ; magic things
call    printf          ; call to printf

Thanks in advance for the support.

Upvotes: 1

Views: 6512

Answers (2)

Appleman1234
Appleman1234

Reputation: 16086

%eax is the value stored in the register

(%eax) is value in memory using the value stored in eax

E.g. movl 4, %eax
This sets the value of eax to 4.

The value of (%eax) is now whatever is located in memory at the address of 4.

movl    (%eax), %eax    ; move the value in memory of eax  (value eax points to) to the address of register eax
movl    %eax, (%esp)    ; move the address of eax to the value in memory of esp (value that esp points to)

movl    %edx, 4(%esp)   ; move the address of edx to the value in memory of esp + 4 
movl    %eax, (%esp)    ; move the address of eax to the value in memory of esp

The reason the first example of yours has just movl %eax, (%esp) is because atoi only takes one argument.

The second example uses movl %edx, 4(%esp) because eax is already being used and printf takes two arguments.

Upvotes: 1

fnl
fnl

Reputation: 2239

The parenthesis are register-indirect addressing (think pointer-dereferencing). number(register) means "add the offset number before dereferencing".

On how to call other functions, see the calling conventions for your system. For x86-32, see this.

Upvotes: 0

Related Questions