给定一个数,求该数的补数
程序运行:
代码:
datas segment
NUM DB 12H,34H,56H,78H,9AH,0BCH,23H,45H
DB 67H,89H,0DEH,13H,24H,35H,46H,57H
N DB $-NUM
outputNUM db "orignal data NUM=$"
outputNUMC db 0dh,0ah,"NUM's complement=$"
datas ends
stacks segment stack
db 100h dup(?)
stacks ends
codes segment
assume cs:codes,ds:datas,ss:stacks
main proc far
start:
push ds
mov ax,0h
push ax
mov ax,datas ;初始化ds
mov ds,ax
;输出原始的NUM
lea dx,outputNUM
mov ah,9
int 21h
mov bl,N
xor bh,bh
dec bx
os:
mov al,NUM[bx]
call hexidemical
dec bx
cmp bx,0
jge os
mov dl,'H'
mov ah,2
int 21h
;求补
mov cl,N ;将NUM的字节数移至cx
xor ch,ch
cmp cx,0 ;判断字节数是否为0
jz no_operate ;若为0,跳转
lea bx,NUM ;取NUM的偏移地址
mov ah,0 ;ah清零
s:
xor al,al ;al设置0
sahf ;恢复上一次减法的标志位
sbb al,[bx] ;带位减法,求补
lahf ;保存标志位
mov [bx],al ;求补的值保存在原始的位置
inc bx ;移至下一个单元
loop s
;输出求补的NUM
lea dx,outputNUMC
mov ah,9
int 21h
mov bl,N
xor bh,bh
dec bx
s1:
mov al,NUM[bx]
call hexidemical
dec bx
cmp bx,0
jge s1
mov dl,'H'
mov ah,2
int 21h
no_operate:
ret
main endp
hexidemical proc near
push ax
push cx
push bx
xor ah,ah
mov bl,16
div bl
mov cx,ax
mov dl,cl
cmp dl,10
jb next
add dl,7
next:
add dl,30h
mov ah,2
int 21h
mov dl,ch
cmp dl,10
jb next1
add dl,7
next1:
add dl,30h
int 21h
pop bx
pop cx
pop ax
ret
hexidemical endp
codes ends
end main