1

I want to transfer string to integer for example, when I type 1234 in string, it will transfer to integer 1234. However, when I type 1234, only 12 comes out as a result and I have no idea what the problem.

%include "asm_io.inc"

segment .bss
string  resb    32


segment .text
global  main

main:

  enter 0,0     ; setup stack frame
  pusha

  mov   edx, 0
  mov   ecx, 0
  mov   ebx, 0

repeat: call    read_char

  sub   eax, 48
  mov   esi, eax
  mov   eax, ecx
  mov   ebx, 10
  mul   ebx
  mov   ecx, eax
  add   ecx, esi
  mov   byte [string+edx], al

  cmp   al, 0x0a
  jne   repeat
  mov   byte [string+edx-1], 0

  mov   eax, ecx
  call  print_int
  call  print_nl

  popa
  mov   eax, 0  ; return value
  leave         ; leave stack frame
  ret
2
  • 1
    GDB is a good debugger. Useful skill to learn is to use a debugger so you can look at memory and registers as you step through the code. Commented May 13, 2017 at 3:12
  • I think there are a couple of issues. You do cmp al, 0x0a . You are trying to test for the newline character but you have overwritten AL by effectively subtracting 48 from it. So you are no longer comparing the original value returned by read_char Commented May 13, 2017 at 3:32

1 Answer 1

2

Just by analysing, without running, it looks like your logic is wrong. On the second loop iteration, you will have eax equal to 1 so after multiplying it by 10 (ebx) you will produce the result that is equal to the ascii value of Enter - 0x0a (10dec).

You should move your check for enter value right after reading the char. So try to have your loop like this

repeat: 
  call  read_char
  cmp   al, 0x0a
  je    exit_loop // exit the loop if enter
  //code as before
  jmp repeat //jump unconditionally to the beginning of the loop
exit_loop:
   mov   byte [string+edx-1], 0

I think there might be some other issue as I don't see where would edx get incremented.

But as I wrote - it's just by analysing w/o actually running. You have the program and the debugger. Debug it! Step through the code, analyse registers and confirm what's going on as Michael Petch suggested.

Sign up to request clarification or add additional context in comments.

Comments

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.