负数MASM输入和输出

时间:2016-11-29 16:50:05

标签: masm x86-16 dos signed

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

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

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

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

1 个答案:

答案 0 :(得分: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