Procedures from C in assembly

209 Views Asked by At

I have to write a short program in Assembly but my version not works. It should print ASCII char, next change it to the integer value by atoi function and print out this value. Important is using for that, procedures from C: puts() and atoi().

What I am doing wrong? Please explain it for me as clear as posible. I'm using gcc and I'm writing in intel_syntax Assembly.

It's my code:

    .intel_syntax noprefix
    .text
    .globl main  

main:

    mov eax, offset msg
    push eax
    call puts
    pop eax

    push eax
    call atoi
    pop eax

    push eax
    call puts
    pop eax

    .data
msg:
    .asciz "a"

Thank you in advance

1

There are 1 best solutions below

1
On BEST ANSWER

Using atoi makes no sense in this context. Are you sure you are supposed to use that?

Assuming you want to print the ascii code instead (which is 97 for a) you could use printf or the non-standard itoa instead.

By the way, you are destroying the return value from atoi by the pop eax. Also, you are missing the ret at the end of main.

Sample code using printf:

main:
    push offset msg
    call puts
    pop eax

    movzx eax, byte ptr [msg]
    push eax
    push offset fmt
    call printf
    add esp, 8   # clean up stack

    xor eax, eax # zero return value
    ret          # return
.data
msg:
    .asciz "a"
fmt:
    .asciz "%d\n"