Assembly Intel ASM添加到EAX

Assembly Intel ASM添加到EAX,assembly,add,nasm,intel,mov,Assembly,Add,Nasm,Intel,Mov,我对ASM还比较陌生。我正在运行ARCH linux 64位,并使用以下命令进行编译,所有操作都运行顺利: nasm -f elf32 -o file.o file.asm ld -s -m elf_i386 -o file file.o 我接受用户输入(十进制),我只想将其加倍。但是,当我使用: add eax, eax 我没有动力。然后我试着: add eax, 1 这将为eax添加1。但是,这会向内存地址添加1。我在.bss部分保留了10个字节 当我输入“1234”时,它输出“234

我对ASM还比较陌生。我正在运行ARCH linux 64位,并使用以下命令进行编译,所有操作都运行顺利:

nasm -f elf32 -o file.o file.asm
ld -s -m elf_i386 -o file file.o
我接受用户输入(十进制),我只想将其加倍。但是,当我使用:

add eax, eax
我没有动力。然后我试着:

add eax, 1
这将为eax添加1。但是,这会向内存地址添加1。我在.bss部分保留了10个字节

当我输入“1234”时,它输出“234”(移位+1字节),而不是“1235”

.bss部分:

    i: resd 
    j: resd 10
完整声明:

    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

您操作的是数字的字符串表示形式

您可能需要:

  • 将字符串转换为数字
  • 加倍
  • 将结果数字转换回字符串
  • 比如(未经测试的代码):

    包含函数的NASM实现

    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