2016-11-29 91 views
0

我正在编写MASM x8086中的一个字节大小的多派生应用程序,它必须能够接收负系数。负数MASM输入和输出

我知道二进制文件可以用有符号和无符号的形式表示。但我正在寻找一种方法来接收一个有符号的整数,以便我可以避免另一个数组。或者有没有办法将我的变量定义为有符号整数?

下面是我的整数输入程序。

TEN db 10 ;;; constant 
num db ? ;;; coefficient 
      ;;; bh is the degree 

get_int PROC 
    lea si, string  ; replaces the ? in the string with the degree 
    add si, 13h 
    mov dl, bh 
    add dl, 30h 
    mov [si], dl 

    mov ah, 09h 
    lea dx, string  ; prompt 
    int 21h 

    mov ah, 0Ah 
    lea dx, buffString ; user input 
    int 21h 

    lea si, buffString ; point to count byte 
    inc si 

    mov ch, 00h  ; cx = count 
    mov cl, [si] 

    add si, cx   ; si points to end of string 

    mov dl, 00h  ; hold result(dl) 
    mov bl, 01h  ; hold 10^x (bl) 

loop1: 
    mov al, [si]  ; prep for char ---> number conversion 
    cmp al, '-' 
    je negativeSign 

    sub al, 30h  ; convert 
    mul bl    ; ax = al*bl 
    add dl, al   ; sum of results 

    mov al, bl   ; preload for instruction 
    mul TEN   ; TEN is variable predefined as 10 
    mov bl, al 
    jmp overNegative 
negativeSign: 

    mov dh, 00h 
    mov [si], dh 
overNegative: 
    dec si 
    loop loop1   ; loop instruction uses cx as index counter once zero breaks 

    mov num, dl 
    ret 
get_int ENDP 
; output is num 
+0

到底是什么'C++'的一部分? –

+0

删除了标签。没有C++。 – TheLiquor

+0

你在哪里处理' - '字符?你使用“二补”吗? – Ripi2

回答

1

当解释输入时,您偶然发现“ - ”字符,这是一个保存假设,您已达到数字的开头。所以你应该退出循环。我没有看到用零代替“ - ”字符的任何意思!

你需要做的是否定以获得正确的符号结果数:

loop1: 
mov al, [si]  ; prep for char ---> number conversion 
cmp al, '-' 
je negativeSign 

sub al, 30h  ; convert 
mul bl    ; ax = al*bl 
add dl, al   ; sum of results 

mov al, bl   ; preload for instruction 
mul TEN   ; TEN is variable predefined as 10 
mov bl, al 
dec si 
loop loop1   ; loop instruction uses cx as index counter once zero breaks 

mov num, dl  ;Positive number [0,127] 
ret 

negativeSign: 
mov dh, 00h  <<<<<<< Need this as a flag? 
neg dl 
mov num, dl  ;Negative number [-128,-1] 
ret