汇编初学者问题
R0,R1两个有符号数,同号时两数绝对值相减,异号时两数绝对值相加,并使结果始终与R0符号相同。麻烦详细讲下思路,有代码最好。
原理:利用补数进行有符号数运算。
.386
;***********************************************************
code segment use16
assume cs:code,ds:code
org 100h
;-----------------------------------------------------------
start: jmp short begin
msg0 db 'Input minuend : $' ;输入被减数与减数,必须
msg1 db 0dh,0ah,'Input subtrahend : $' ;在-2147483648与
msg2 db 7,'Input error!$' ;2147483647之间
answer db 0dh,0ah,40 dup (' ')
buffer db 12,13 dup (0)
n1 dd ?
n2 dd ?
begin: mov dx,offset msg0
call input
jc error
mov n1,edx
mov di,offset answer+2
call hex2dec
mov al,'-'
stosb
mov dx,offset msg1
call input
jc error
mov n2,edx
call hex2dec
mov al,'='
stosb
mov edx,n1
sub edx,n2
call hex2dec
mov al,'$'
stosb
mov dx,offset answer
jmp short ok
error: mov dx,offset msg2
ok: mov ah,9
int 21h
mov ax,4c00h
int 21h
;-----------------------------------------------------------
input proc near
mov ah,9
int 21h
mov dx,offset buffer
mov ah,0ah
int 21h
sub eax,eax
mov cl,'+'
mov edx,eax
mov si,offset buffer+2
ipt0: lodsb
cmp al,'-'
jne ipt1
mov cl,al
jmp ipt0
ipt1: cmp al,'+'
je ipt0
cmp al,0dh
je ipt2
cmp al,'0'
jb ipt4
cmp al,'9'
ja ipt4
sub al,'0'
shl edx,1
mov ebx,edx
shl edx,2
add edx,ebx
add edx,eax
jmp ipt0
ipt2: cmp cl,'-'
jne ipt3
neg edx
ipt3: clc
jmp short ipt5
ipt4: stc
ipt5: ret
input endp
;-----------------------------------------------------------
hex2dec proc near
jmp short h2d0
tmp db 10 dup (' ')
h2d0: test edx,80000000h ;检查是否为负
jz h2d1
mov al,'-'
neg edx
stosb
h2d1: push di
mov ebx,10
mov eax,edx
mov di,offset tmp+9
mov cx,bx
h2d2: sub edx,edx
div ebx
add dl,'0'
mov [di],dl
or eax,eax
jz h2d3
dec di
loop h2d2
h2d3: mov si,di
pop di
sub bx,cx
mov cx,bx
inc cx
rep movsb
ret
hex2dec endp
;-----------------------------------------------------------
code ends
;***********************************************************
end start
以前刚学汇编练手的代码,原作者记不清了。感谢他的代码。
[ 本帖最后由 东海一鱼 于 2010-7-2 17:36 编辑 ]