-4

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

Jester
  • 56,577
  • 4
  • 81
  • 125
  • 1
    Questions like this should be tagged with the correct architecture. Also, where are you stuck? As written here, this isn't a question. – ComputerDruid Dec 15 '14 at 14:50
  • Provide more information. What is the problem ? What compiler/assembler are you using ? What is the architecture ? What OS are you using ? Depending on your problem, that could change a lot. Guesswork can be tiring... – ElderBug Dec 15 '14 at 14:54
  • I correct my question. I'm sorry for that but I'm newbie in Assembly – Darayavahos Dec 15 '14 at 15:06
  • You still haven't told your problem. Can you compile this or does this crash at runtime ? And what error are you getting ? Knowing the target (gcc -v) and the OS would help too. – ElderBug Dec 15 '14 at 15:24
  • It works but no proper as I wrote. – Darayavahos Dec 15 '14 at 16:00
  • I don't know why you still ask about OS version? it's x86 as you can see specific types of mnemonics. – Darayavahos Dec 15 '14 at 16:02
  • I'm asking that because, if I compile your code right now, it will compile fine but it will never work because it will compile as amd64 with MS calling convention. – ElderBug Dec 15 '14 at 16:33
  • Ok so I'm using linux x86 and I compile it with gcc – Darayavahos Dec 15 '14 at 16:35

1 Answers1

0

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"
Jester
  • 56,577
  • 4
  • 81
  • 125
  • Yes I'm supposed to used atoi and puts only. Because the next part of my task is convert char '5' to integer and next do an arithmetical operations on it. – Darayavahos Dec 15 '14 at 15:59