3

Let me explain it briefly :).

I approach the argument 10, by using EBP register with ebx register because of the stack structure of which is containing EBP(base register), return address, # of parameter, parameter 1, parameter 2... since I don't use a local register. I could see the parameter was appropriately input as I could print it using call print_string. But, since < while: > code line, it seems like the string 10 doesn't seem to be read for the commend line does nothing when it comes to it. I would gently ask where to start with the code. thanks for reading.

Input : ./atoi 10
Result : 10

%include "asm_io.inc"

    segment .data

    segment .bss
    input   resw 1

    segment .text
    global  main
main:
    enter   0,0
    pusha
    mov ebx, [ebp+12]
    mov eax, [ebx+4]
     ; call print_string
    dump_stack 1,2,4
    mov ebx, 0      
    mov ecx, 10
while:
    cmp al, 0x0a
    je print
    sub eax, 0x30
    mov [input], eax
    mov eax, ebx
    mul ecx
    add eax, [input]
    mov ebx, eax
    jmp while
print:
    mov eax, ebx
    call print_int
    call print_nl

    popa
    mov     eax, 0 
    leave
    ret
5
  • eax is a pointer. You need to 1) dereference and 2) increment it. Also make sure you use the correct operand sizes (a char is 1 byte). Commented Jun 5, 2016 at 12:05
  • Replacing eax to %eax and resw to resb are the solution you mean? Commented Jun 5, 2016 at 12:30
  • 1
    %eax is at&t syntax and even there it doesn't dereference. I even missed input, that's defined as word which is doubly wrong. Yeah resb would be better but then of course you can't store 32 bits into it. Not that I understand why you need that at all. Commented Jun 5, 2016 at 12:37
  • Input is used for the space to sum integer numbers out of string which is created by subtraction like 'A'-'a'. ( you can think of atoi function. ). Commented Jun 5, 2016 at 12:51
  • Okay then use whatever is appropriate for the range of numbers you want to process. But make sure you use the proper size to access it. Commented Jun 5, 2016 at 13:14

1 Answer 1

2

Your while loop doesn't read any characters! You retrieve these using mov dl,[eax].
As you can see from the code below, there's no need to use a temporary input variable.

  xor   ebx, ebx            ;Result
while:
  movzx edx, byte ptr [eax] ;Read 1 character
  test  dl, dl              ;Test for end of string
  jz    print               ;End found
  sub   dl, 0x30            ;Go from character to value [0,9]
  imul  ebx, 10             ;Result x10
  add   ebx, edx            ;Add new digit
  inc   eax                 ;To next character
  jmp   while
Sign up to request clarification or add additional context in comments.

5 Comments

xor ebx, ebx ;Result
sorry bro I don't get it, can you explain a little more? BTW These code don't work
xor ebx,ebx is meant to clear the EBX register. Just like mov ebx,0 does but the instruction is better.
@jay: did you try single-stepping your code in a debugger? That usually helps.
Late catch. Aren't these strings supposed to end with a terminating zero? Then instead of comparing with 0x0a use cmp dl,0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.