Wang Shuang assembly language Part VIII (Functions write, call and ret used in conjunction)

Problem: When using div do division, there may be the result of too much business over the scope of the register stored, this error is called divide overflow.

There is a formula to solve the division overflow:

X: dividend, range: [0, FFFFFFFF]
N: divisor, range: [0, FFFF]
H: X-high 16 range: [0, FFFF]
L: X-Low 16, range: [0, FFFF]

int (): Descriptive operators, providers take, for example, int (38/10) =. 3
REM (): Description of the operator, taking the remainder, for example, rem (38/10) = 8

Formula: X / N = int (H / N) * 65536 + [rem (H / N) * 65536 + L] / N

Solution:
1, its argument is the high data and low data divisor

2. Results:
's high as int (H / N) * 65536
's low of [rem (H / N) * 65536 + L] List / N,
and the remainder is [rem (H / N) * 65536 + L] / N remainder

3, the process of:
(1) First, the high pass data ax, incoming divisor cx, then div cx,
AX the value of int (H / N), can be obtained high results
(2) Then, the value of dx is rem (H / N), multiplied by 65536 is equivalent to a left 16-bit, 16-bit equivalent to a 32-bit high data usage div DX is the presence of high dividend, the presence of low dividend AX, so this time dx has been saved order data
(3) Finally, the low incoming data AX, div cx, business is the result of the low, the remainder is the result of the remainder.

Write code:
the main function

;计算1000000/10=F4240H/0AH,结果为186A0H

 mov ax,4240h;数据的低位
 mov dx,0fh;数据的高位
 mov cx,0ah;除数
 call divdw;调用子函数
 mov ax,4c00h
 int 21h

Functions for the

divdw:
 push si
 push bx;保存现场
 mov bx,ax
 mov ax,dx;把数据的高位传入AX
 mov dx,0
 div cx;结果AX的数值为结果的高位,DX的数值为rem(H/N)
 mov si,ax
 mov ax,bx;把数据的低位传入AX
 div cx;结果AX的数值为结果的低位,DX的数值为结果的余数
 mov cx,dx
 mov dx,si
 pop bx;恢复现场
 pop si
ret;函数返回

call instruction

The call instruction process is executed
(1) or the current IP CS and IP onto the stack
(2) Transfer

call reference
corresponds
Push the IP
JMP near PTR numeral

call far ptr label
is equivalent to
the Push CS
the Push IP
jmp FAR ptr label

call word ptr address of memory
corresponding to
the IP Push
Word PTR address of memory jmp
example:

mov sp,10h
mov ax,0123h
mov ds:[0],ax
call word ptr ds:[0]
;执行后,(IP)=0123h,(sp)=0EH

call dword ptr address of memory
corresponding to
Push the CS
Push the IP
JMP DWORD PTR address of memory

example:

mov sp,10h
mov ax,0123h
mov ds:[0],ax
mov word ptr ds:[2],0
call dword ptr ds:[0]
;执行后,(CS)=0,(IP)=0123h,(sp)=0cH

ret and retf

ret instruction data stack, the content of IP, enabling the transfer of nearly
retf instruction data stack, modify the contents of CS and IP, enabling transfer away

with the use of call and ret
frame having a source subprogram follows:

assume cs:code
code segment
 main:
 call sub1
 :
 :
 mov ax,4c00h
 int 21h
sub1:
:
:
call sub2
:
:
ret

sub2:
:
:
ret
code ends
end main
Released eight original articles · won praise 0 · Views 78

Guess you like

Origin blog.csdn.net/qq_41955402/article/details/104309070