0

I'm really new to NASM and I'm trying to convert from string to number in NASM but I don't know if it works. The code compiles without a problem but it's always displaying the same result no matter the numbers

Can someone take a look to see if anything is wrong?

string_int:
xor rbx, rbx ; result saved here
xor rax, rax
xor rcx, rcx ; counter

_loop:
mov al, byte[rsi + rcx]
cmp al, 0xA
je done
inc rcx
sub al, 0x30
mov rax, 0xA
mul rbx
add rbx, rax

jmp _loop

done:
ret
Frodo
  • 21
  • 5
  • You destroy your value in AL with `mov rax, 0xa`. You would have found this yourself if you single-stepped with a debugger and watched registers change. You probably meant RBX. Or better, `imul eax, 0xa` / `add ebx, eax` because you're not limited to 8086 instructions like one-operand MUL or IMUL. (Or even better, two LEA instructions can multiply by 10 and add with lower latency than IMUL-immediate.) – Peter Cordes Oct 26 '18 at 02:08
  • Actually you want `imul rbx, 10` to implement `total = 10*total + digit`, I got that backwards. – Peter Cordes Oct 26 '18 at 02:16
  • Here's how to do it efficiently: [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) – Peter Cordes Oct 26 '18 at 02:17

0 Answers0