Skip to content

Instantly share code, notes, and snippets.

@Ionizing
Created January 16, 2018 14:18
Show Gist options
  • Save Ionizing/bcb9e4f22a208aa8855993dc24ea18a7 to your computer and use it in GitHub Desktop.
Save Ionizing/bcb9e4f22a208aa8855993dc24ea18a7 to your computer and use it in GitHub Desktop.
王爽《汇编语言》实验10 不会产生溢出的除法
;返回结果 dx=结果高16位,ax=结果低16位,cx=余数
assume cs:code, ss:stack
stack segment
dd 10h dup(0)
stack ends
code segment
start:
mov ax, stack
mov ss, ax
mov sp, 40h
;被除数 DWORD
mov ax, 4240h ;被除数低16位 WORD
mov dx, 000fh ;被除数高16位 WORD
mov cx, 0ah ;除数 WORD
call divdw
mov ah, 4ch
int 21h
;公式: X/N = INT(H/N)*65536 + [REM(H/N)*65536 + L]/N
divdw: ;push cx
push ax ;转存AX,低16位
mov ax, dx ;对高16位做除法
mov dx, 0
div cx ;AX 存商,DX 存余数,恰好dx要乘以65536(10000H)
;可以直接将他放在dx中表示被除数的高位,相当于自动乘以10000h
mov bx, ax
pop ax ;被除数低16位
div cx ; / N
mov cx, dx ;remains
mov dx, bx
ret
code ends
end start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment