2017-01-18 19 views
-3

下面的代码无法正常工作如何检查给定的数字是i386的汇编语言正或负

cmp ax, 0 jl NegativeNumber ;jump to negative statements

test ax , 0x80 ;to compare sign bit. but it doesn't work 

汇编不会跳到NegativeNumber过程中,虽然输入的号码负。

以下是我的实际代码

section .data 
str1:db 10,13, 'Enter the number' 
str1len equ 19 
str2: db 10,13, 'The number is positive' 
str2len equ 22 
str3 : db 10, 13, 'The number is negative' 
str3len equ 22 

section .bss 

    num resw 1 
    cbuff resw 1 


global _start 
section .text 
_start: 

%macro sys_read 2 
    mov rax,0 
    mov rdi,0 
    mov rsi,%1 
    mov rdx,%2 
    syscall 
%endmacro 

%macro sys_write 2 
    mov rax,1 
    mov rdi,1 
    mov rsi,%1 
    mov rdx,%2 
    syscall 

%endmacro 

    call GetNumber 



GetNumber: 
    sys_write str1, str1len 
    sys_read cbuff , 1 
    mov ax , [cbuff] 
    cmp ax , 0h 
    jl NumberNegative 
     sys_write str2, str2len 
     ret 

NumberNegative: 

    sys_write str3, str3len 
     ret 

是否有任何其他的方法来确定给定的数字,而无需使用标志正面还是负面的? 我在做错事吗? 顺便说一句,我在Linux ubuntu上使用NASM。

+1

什么是'as'?你的意思是'斧头'?另外,如果你正在处理16位数字,你应该检查位15,而不是位7(或者只是'测试ax,ax','js Negative')。 – Michael

+0

是的。抱歉。它的斧头。我现在纠正了问题。好的,你能告诉我如何检查15位?谢谢 – MCCshreyas

+1

2^15 = 0x8000,使用。 –

回答

1

在线NASM使用Linux:http://www.tutorialspoint.com/compile_assembly_online.php

例子:

section .text 
    global _start  ;must be declared for using gcc 
_start:      ;tell linker entry point 
    mov  ax,-4  ; test value 

doFewTests: 
    push eax 

    cmp  ax,0 
; test ax,ax 
; test ax,0x8000 

    jl  handleNegative 
    ; handle non-negative 
    ; fake conversion to ASCII for numbers 0-9 
    add  al,'0' 
    mov  ecx,strPositive 
    mov  edx,lenPositive 
    mov  [ecx+edx-2],al 
    jmp  printMessage 
handleNegative: 
    ; fake conversion to ASCII for numbers -9 to -1 
    neg  al 
    add  al,'0' 
    mov  ecx,strNegative 
    mov  edx,lenNegative 
    mov  [ecx+edx-2],al 
printMessage: 
    mov  ebx, 1  ;file descriptor (stdout) 
    mov  eax, 4  ;system call number (sys_write) 
    int  0x80  ;call kernel 
    pop  eax 
    inc  ax 
    cmp  ax,5 
    jl  doFewTests ; do -4 to +4 demonstration loop 
    ; exit 
    mov  eax, 1  ;system call number (sys_exit) 
    int  0x80  ;call kernel 

section .data 

strPositive db 'Positive number: x', 10 
lenPositive equ $ - strPositive 
strNegative db 'Negative number: -x', 10 
lenNegative equ $ - strNegative 

顺便说一句,有没有点而不调试工作,而你显然不听。本网站仅适用于演示最后一个示例,因为它没有安装调试器,因此无法学习Assembly编程。

如果你不听,你只是寻找简单的方法,你永远不会学习大会,因为在大会只有“适当的”方式,不容易的。

最后时间:使用调试器。

如果您不知道如何使用https://stackoverflow.com/tags/x86/info底部的说明进入gdb,然后找到其他教程/文档如何使用它。

最终尝试一些图形前端像ddd(但我最后一次尝试,体验不是很好,学习控制gdb是GNU世界长期投资,作为调试器也使用其它高级语言,所以不要犹豫,花DAYS就可以了。

(我使用edb调试器,但我不得不从源代码编译,所以这可能是另一种“可以蠕虫”你)。


该fuglyexa的产量mple:

sh-4.3$ nasm -f elf *.asm; ld -m elf_i386 -s -o demo *.o 
sh-4.3$ demo 
Negative number: -4 
Negative number: -3 
Negative number: -2 
Negative number: -1 
Positive number: 0 
Positive number: 1 
Positive number: 2 
Positive number: 3 
Positive number: 4 
+0

谢谢老兄。它终于工作......... – MCCshreyas