实验3 转移指令跳转原理及其简单应用编程

1. 实验任务1

使用任何一款文本编辑器,录入8086汇编程序源码task1.asm

task1.asm

assume cs:code, ds:data
data segment
x db 1, 9, 3
len1 equ $ - x ; 符号常量, $指下一个数据项的偏移地址,这个示例中,是3
y dw 1, 9, 3
len2 equ $ - y ; 符号常量, $指下一个数据项的偏移地址,这个示例中,是9
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, offset x ; 取符号x对应的偏移地址0 -> si
mov cx, len1 ; 从符号x开始的连续字节数据项个数 -> cx
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 ; 取符号y对应的偏移地址3 -> si
mov cx, len2/2 ; 从符号y开始的连续字数据项个数 -> cx
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

对源程序进行汇编、链接,得到可执行程序task1.exe,运行后,结合运行结果和注释,及必要的debug 调试:

 

 1. 理解运算符offset、伪指令equ、预定义符号$的灵活使用。

通过line5、line8,以及数据项的数据属性(字节、字、双字,等),可以方便计算出连续数据项 的个数,而无需人工计数。

注*: 符号常量len1, len2不占用数据段内存空间

2. 回答问题

 ① line27, 汇编指令 loop s1 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机器码,分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明是如何计算得到跳转后标号s1其后指令的偏移地址的。

14,CPU将跳转位移量与IP值相加,得到下一条要执行指令的IP


② line44,汇编指令 loop s2 跳转时,是根据位移量跳转的。通过debug反汇编,查看其机器码,
分析其跳转的位移量是多少?(位移量数值以十进制数值回答)从CPU的角度,说明是如何计算得
到跳转后标号s2其后指令的偏移地址的。

-16,0039到0029

 

2. 实验任务2

使用任何一款文本编辑器,录入8086汇编程序源码task2.asm。

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

① 根据call指令的跳转原理,先从理论上分析,程序执行到退出(line31)之前,寄存器(ax) = 0021 寄存器 (bx) =0026  寄存器(cx) = 076C

② 对源程序进行汇编、链接,得到可执行程序task2.exe。使用debug调试,观察、验证调试结果与理论 分析结果是否一致。

 

 

3. 实验任务3 针对8086CPU,已知逻辑段定义如下:

data segment
x db 99, 72, 85, 63, 89, 97, 55
len equ $- x
data ends

编写8086汇编源程序task3.asm,在屏幕上以十进制形式输出data段中这一组连续的数据,数据和数据之间以空格间隔。

assume cs:code, ds:data

data segment
    x db 99,72,85,63,89,97,55
    len equ $-x
data ends

code segment ;
start:
    mov ax,data
    mov ds,ax
    mov byte ptr bl,10
    mov si,offset x
    mov cx,len 
s:  mov al,ds:[si]
    mov ah,0
    div bl
    call printNumber
    call printSpace
    inc si
    loop s
    mov ah,4ch
    int 21h

printNumber:
    mov dx,ax
    mov ah,2
    or dl,30h
    int 21h
    mov dl,dh
    or dl,30h
    int 21h
    ret

printSpace:
    mov ah,2
    mov dl,' '
    int 21h
    ret

code ends
end start

 

 

4. 实验任务4

针对8086CPU,已知逻辑段定义如下:

data segment
str db 'try'
len equ $ - str
data ends

编写8086汇编源程序task4.asm,在屏幕上以指定颜色、指定行,在屏幕上输出字符串。

assume cs:code, ds:data

data segment
    str db 'try'
    len equ $ - str
data ends

code segment
start:
    mov ax, data
    mov ds, ax
    mov ax, 0b800h
    mov es, ax

    mov si, offset str
    mov bl,    2h    
    mov bh,    0    
    call printStr

    mov si, offset str
    mov bl, 4h
    mov bh, 24
    call printStr

    mov ah, 4ch
    int 21h

printStr:
    mov al, bh
    mov dl, 00A0h
    mul dl

    mov di, ax
    mov cx, len  
s:
    mov al, ds:[si]
    mov es:[di], al
    inc di
    mov es:[di], bl
    inc si
    inc di
    loop s
    ret

code ends
end start

 

 5. 实验任务5

针对8086CPU,针对8086CPU,已知逻辑段定义如下:

data segment
stu_no db '20498329042'
len = $ - stu_no
data ends

在80×25彩色字符模式下,在屏幕最后一行正中间显示学号。要求输出窗口蓝底,学号和两侧折线,以 白色前景色显示。

assume cs:code,ds:data

data segment
    stu_no db '201983290204'
    len = $-stu_no
data ends

stack segment
    db 16 dup(0)
stack ends

code segment
start:
    mov ax,data
    mov ds,ax
    mov ax,stack
    mov ss,ax
    mov sp,16
    mov si,0

    call setColor
    call setLine
    call setNo

    mov ah,4ch
    int 21h

setColor:
    mov ax,0b800h
    mov es,ax
    mov ax,0
    mov al,25
    mov dl,80
    mul dl
    mov di,0
    mov bl,00010000B
    mov dl,20h 
    mov cx,ax   
s:  mov es:[di],dl
    inc di
    mov es:[di],bl
    inc di
    loop s
    ret

setLine:
    mov ax,0b800h
    mov es,ax
    mov ax,0
    mov al,24
    mov dx,160
    mul dx
    mov di,ax
    mov dl,2dh
    mov bl,00010111B
    mov cx,80
p:  mov es:[di],dl
    inc di
    mov es:[di],bl
    inc di
    loop p
    ret

setNo:
    mov ax,0b800h
    mov es,ax
    mov ax,0
    mov al,24
    mov dx,160
    mul dx
    mov di,ax
    mov ax,68
    add di,ax
    mov bl,00010111B
    mov cx,len
q:  mov ax,ds:[si]
    mov es:[di],ax
    inc di
    mov es:[di],bl
    inc di
    inc si
    loop q
    ret
code ends
end start

 

posted @ 2021-11-29 21:24  Aost  阅读(11)  评论(1编辑  收藏  举报