Intel ASM Adding to EAX

1.5k Views Asked by At

I'm fairly new to ASM. I'm running ARCH linux 64 bit and use the following commands to compiule and everything runs smoothly:

nasm -f elf32 -o file.o file.asm
ld -s -m elf_i386 -o file file.o

I am taking user input (as a decimal) and I simply want to double it. However, when I use:

add eax, eax

I get no ouput. I then tried:

add eax, 1

Which should add 1 to eax. However, this adds 1 to the memory address. I have 10 bytes reserved in my .bss section.

When I type "1234" it ouputs "234" (shifted +1 byte) as opposed to "1235"

.bss section:

    i: resd 
    j: resd 10

Full statement:

    mov eax, 3 ;syscall
    mov ebx, 2 ;syscall
    mov ecx, i ;input variable
    mov edx, 10 ;length of 'i'
    int 0x80 ;kernel call

    mov eax, i ;moving i into eax
    add eax, 0x1 ;adding 1 to eax
    mov [j], eax ;moving eax into j
1

There are 1 best solutions below

0
On

What you are operating on is a string representation of the number.

You probably need to:

  1. Convert the string to a number
  2. Double the number
  3. Convert the resulting number back to a string

So something like (untested code ahead):

mov esi,i ; get address of input string
; convert string to a number stored in ecx
xor ecx,ecx ; number is initially 0
loop_top:
mov bl,[esi] ; load next input byte
cmp bl,0 ; byte value 0 marks end of string
je loop_end
sub bl,'0' ; convert from ASCII digit to numeric byte
movzx ebx,bl ; convert bl to a dword
mov eax,10 ; multiply previous ecx by 10
mul ecx
mov ecx,eax
add ecx,ebx ; add next digit value
inc esi ; prepare to fetch next digit
jmp loop_top
loop_end:
; ecx now contains the number
add ecx,ecx ; double it
; to do: convert ecx back to a string value

Page 22 of this document contains a NASM implementation of the atoi function.