汇编语言(二十三)之求一个数的补数

给定一个数,求该数的补数

程序运行:

 

代码:


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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值