2014-01-07 44 views
0

我對ASM相當陌生。我運行的Arch Linux 64位,並使用以下命令來compiule和一切順利:Intel ASM添加到EAX

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

我以用戶輸入(如小數),我只是想加倍。但是,當我使用:

add eax, eax 

我得不到輸出。然後,我嘗試:

add eax, 1 

哪個應該加1到eax。但是,這會將內存地址加1。我的.bss部分保留了10個字節。

當我鍵入 「1234」 它。OUPUTS 「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 

回答

0

您正在操作的是數字的字符串表示形式。

你可能需要:

  1. 將字符串爲數字
  2. 雙數
  3. 轉換得到的數字回字符串

因此,像(未經測試的代碼提前):

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包含atoi函數的NASM實現。