实验3 转移指令跳转原理及其简单应用编程
实验一
源码及截图
assume cs:code, ds:data
data segment
x db 1, 9, 3
len1 equ $ - x
y dw 1, 9, 3
len2 equ $ - y
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, offset x
mov cx, len1
mov ah, 2
s1:mov dl, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h
inc si
loop s1
mov ah, 2
mov dl, 0ah
int 21h
mov si, offset y
mov cx, len2/2
mov ah, 2
s2:mov dx, [si]
or dl, 30h
int 21h
mov dl, ' '
int 21h
add si, 2
loop s2
mov ah, 4ch
int 21h
code ends
end start

问题 1
loop s1 跳转的位移量为 -14;
loop s2 跳转的位移量为 -16;
执行 loop 后,CPU 只是简单地将偏移量加到 IP 上。

见 「Intel® 64 and IA-32 Architectures Software Developer’s Manual」LOOP
The target instruction is specified with a relative offset (a signed offset relative to the current value of the instruction pointer in the IP/EIP/RIP register). This offset is generally specified as a label in assembly code, but at the machine code level, it is encoded as a signed, 8-bit immediate value, which is added to the instruction pointer. Offsets of –128 to +127 are allowed with this instruction.
实验二
源码
assume cs:code, ds:data
data segment
dw 200h, 0h, 230h, 0h
data ends
stack segment
db 16 dup(0)
stack ends
code segment
start:
mov ax, data
mov ds, ax
mov word ptr ds:[0], offset s1
mov word ptr ds:[2], offset s2
mov ds:[4], cs
mov ax, stack
mov ss, ax
mov sp, 16
call word ptr ds:[0]
s1: pop ax
call dword ptr ds:[2]
s2: pop bx
pop cx
mov ah, 4ch
int 21h
code ends
end start
ax = 21, bx = 26, cx = 76E

结论一致。
实验三
assume cs:code, ds:data
data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ $- x
data ends
stack segment
db 16 dup(0)
stack ends
code segment
; args
; ax input two-digit
printNumber:
mov bl, 10
div bl
mov bl, ah
mov ah, 2
mov dl, al
add dl, '0'
int 21h
mov dl, bl
add dl, '0'
int 21h
ret
printSpace:
mov dl, ' '
mov ah, 2
int 21h
ret
start:
mov ax, data
mov ds, ax
mov cx, len
mov si, offset x
lp: mov ax, 0
mov al, byte ptr [si]
call printNumber
call printSpace
inc si
loop lp
mov ah, 4ch
int 21h
code ends
end start

实验四
assume cs:code, ds:data
data segment
pstr db 'try'
len equ $ - pstr
data ends
stack segment
db 16 dup(0)
stack ends
code segment
; args
; ds:si ptr to string
; cx len of string
; bl color of output
; bh line number of output, in range [0, 24]
printStr:
mov ax, 0B800H
mov es, ax
mov ax, 0
mov al, 160
mul bh
mov di, ax
printStr_lp1:
mov ax, [si]
mov es:[di], ax
mov es:[di+1], bl
inc si
add di, 2
loop printStr_lp1
ret
start:
mov ax, data
mov ds, ax
mov si, offset pstr
mov cx, len
mov bl, 2h ; green on black
mov bh, 0
call printStr
mov si, offset pstr
mov cx, len
mov bl, 4h ; red on black
mov bh, 24
call printStr
mov ah, 4ch
int 21h
code ends
end start

实验五
assume cs:code, ds:data
data segment
stu_no db '201913360022'
len = $ - stu_no
data ends
stack segment
db 16 dup(0)
stack ends
code segment
; args
; ds:si ptr to string
; cx len of string
; bl color of output
; bh line number of output, in range [0, 24]
start:
mov ax, data
mov ds, ax
; 80*25 = 2000
mov ax, 0B800H
mov es, ax
mov di, 0
mov cx, 2000
mov ax, 17H ; white on blue
lp1:mov es:[di+1], ax
add di, 2
loop lp1 ; fill background
mov ax, 0
mov al, 160
mov bh, 24
mul bh
mov di, ax
mov cx, 80
mov bl, '-'
lp2:mov es:[di], bl
add di, 2
loop lp2 ; fill '-'
mov di, ax
add di, 34*2
mov si, offset stu_no
mov cx, len
lp3:mov al, [si]
mov es:[di], al
inc si
add di, 2
loop lp3 ; fill stu_no
mov ah, 4ch
int 21h
code ends
end start


浙公网安备 33010602011771号