NASM X86 read_int 08 vs 8

时间:2018-03-29 15:28:25

标签: assembly input x86 nasm

我编辑了帖子,所以跳到 EDIT1 查看当前版本。

我制作了一个程序,用二进制打印出特定的斐波那契数字(如果我输入的是5,它会输出第5个斐波纳契数,即5,所以输出为0000 0000 0000 0101b)。我面临的问题是,如果输入为8,则输出为13(10101b),但如果输入为08,则输出为1111001000111011b。

%include "asm_io.inc"

segment .data
    zero_msg db "0000000000000000",0
    one_msg db  "0000000000000001",0
    invalid_msg db "Invalid input 0000_0000",0

segment .text
    global  _asm_main

_asm_main:
    enter 0,0
    pusha

    call read_int
    mov edx,eax
    cmp eax, 0
    je zero
    cmp eax, 1
    je one
    cmp eax, 100
    jge invalid_input
    call fibonacci

    popa
    leave
    ret

invalid_input:
    mov eax,invalid_msg
    call print_string
    jmp end
zero:
    mov eax, zero_msg
    call print_string
    jmp end
one:
    mov eax,one_msg
    call print_string
    jmp end

print_binary:
    shl ecx, 1
    jc print1
    mov eax,0
    call print_int
    inc ebx
    cmp ebx,16
    jne print_binary
    jmp end

print1:
    mov eax,1
    call print_int
    je end
    jmp print_binary


%define fib_hi  dword [ebp-4]
%define fib_lo  dword [ebp-8]
fibonacci:
    enter   16,0            
    push    ebx

    mov   ecx, 1            
    mov   fib_lo,1        
    mov   fib_hi,1       
fib_loop:
    mov   eax,fib_hi    
    mov   ebx,fib_lo
    add   ebx,eax
    mov   fib_lo,eax
    mov   fib_hi,ebx
    inc   ecx
    cmp   ecx,edx
    jne   fib_loop
    mov   ecx,eax
    mov   ebx,0
    shl   ecx,16 
    jmp   print_binary
    end:
      mov   eax,'b'
      call  print_char    
      pop   ebx
      leave                  
      ret

read_int来自我给出的asm_io.asm文件。这就是它的样子。

read_int:
    enter   4,0
    pusha
    pushf

    lea eax, [ebp-4]
    push    eax
    push    dword int_format
    call    _scanf
    pop ecx
    pop ecx

    popf
    popa
    mov eax, [ebp-4]
    leave
    ret

EDIT1:所以我把它做得更干净了,除了08和09之外它还运行良好。我意识到如果我的输入是08或09,那么cpu / nasm / ...(不要不知道哪个)认为我的输入是0.我用print_string命令测试它。 这是现在的代码:

%include "asm_io.inc"

segment .data
    invalid_msg db "Invalid input 0000_0000",0

segment .text
    global  _asm_main
_asm_main:
    enter 0,0
    pusha

    call read_int
    mov edx,eax
    cmp eax,0
    js invalid_input        
    je zero
    cmp eax,1
    je one
    cmp eax,100
    jge invalid_input
    call fibonacci

    popa
    leave
    ret
invalid_input:
    mov eax,invalid_msg
    call print_string
    jmp end
zero:
    mov eax,0
    jmp shift
one:
    mov eax,1
    jmp shift   
print_binary:
    inc ebx
    cmp ebx,17
    je end
    shl ecx,1
    jnc print_0
    mov eax,1
    call print_int
    cmp ebx,17
    jne print_binary
    jmp end
print_0:
    mov eax,0
    call print_int
    jmp print_binary

%define fib_hi  dword [ebp-4]
%define fib_lo  dword [ebp-8]

fibonacci:
    enter   8,0            
    push    ebx

    mov   ecx,0         
    mov   fib_lo,0        
    mov   fib_hi,1       
fib_loop:
    mov   eax,fib_hi    
    mov   ebx,fib_lo
    add   ebx,eax
    mov   fib_lo,eax
    mov   fib_hi,ebx
    inc   ecx
    cmp   ecx,edx
    jne   fib_loop
shift:
    mov   ecx,eax
    mov   ebx,0
    shl   ecx,16
    jmp   print_binary
end:
    mov eax,'b'
    call print_char   

    pop ebx
    leave                  
    ret

0 个答案:

没有答案