实验4 8086标志寄存器及中断

实验四

1. 实验任务1

验证性实验:有些汇编指令会影响到标志寄存器中的一个或多个状态标志位。

在debug环境中,分别实践、观察:

源码

assume cs:code, ds:data

data segment
  x dw 1020h, 2240h, 9522h, 5060h, 3359h, 6652h, 2530h, 7031h
  y dw 3210h, 5510h, 6066h, 5121h, 8801h, 6210h, 7119h, 3912h
data ends
code segment
start:
   mov ax, data
   mov ds, ax
   mov si, offset x
   mov di, offset y
   call add128

   mov ah, 4ch
   int 21h

add128:
   push ax
   push cx
   push si
   push di

   sub ax, ax

   mov cx, 8
s: mov ax, [si]
   adc ax, [di]
   mov [si], ax

   inc si
   inc si
   inc di
   inc di
   loop s

   pop di
   pop si
   pop cx
   pop ax
   ret
code ends
end start

 

image-20211213092557814

image-20211213092651098

观察上图可知

① add指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)是否有影响?

add指令对ZF,CF均有影响。

② inc指令对标志寄存器中的零标志位ZF(Zero Flag)、进位标志位CF(Carry Flag)是否有影响?

使用任意文本编辑器,录入8086汇编源码task1.asm。

inc指令对ZF有影响,但对CF无影响

 

① line31~line34的4条inc指令,能否替换成如下代码?你的结论的依据/理由是什么?

add si, 2 
add di, 2

不能

因为如果使用add会导致进位寄存器CF的值发生变化。

如果本来应当是有进位的,CF的值为CY(1),但是做了add操作后CF会变成NC(0),会对后面的位数加法产生影响。所以不能使用add

inc指令并不影标志寄存器CF的值

 

② 在debug中调试,观察数据段中做128位加之前,和,加之后,数据段的值的变化。

image-20211213100848963

数据被正确求值

 

2. 实验任务2

此部分书写内容:

程序task2.asm源码

assume cs:code, ds:data
data segment
       str db 80 dup(?)
data ends

code segment
start:  
       mov ax, data
       mov ds, ax
       mov si, 0
s1:        
       mov ah, 1
       int 21h
       mov [si], al
       cmp al, '#'
       je next
       inc si
       jmp s1
next:
       mov ah, 2
       mov dl, 0ah
       int 21h
       
       mov cx, si
       mov si, 0
s2:     mov ah, 2
       mov dl, [si]
       int 21h
       inc si
       loop s2

       mov ah, 4ch
       int 21h
code ends
end start

运行测试截图

image-20211213215546834

回答问题

运行程序,从键盘上输入一串字符,以#结束(比如,输入George Orwell, 1984#),观察结

果。结合运行结果,理解代码并回答问题:

① 汇编指令代码line11-18,实现的功能是?

功能:一个一个输入字符直到#结束

② 汇编指令代码line20-22,实现的功能是?

功能:打印一个换行符

 

3. 实验任务3

此部分书写内容:

task3.asm源码

assume cs:code,ds:data,ss:stack
data segment
x dw 91, 792, 8536, 65521, 2021  ;0 1 2 3 4
len equ $ - x  ;$指下一个数据项的偏移地址=5
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,offset x
mov cx,5
print:
push cx
mov cx,0
mov ax,ds:[si]
call printNumber
call printSpace
pop cx
inc si
inc si
loop print

mov ah,4ch
int 21h


printNumber:
mov dx,0
mov bx,10
div bx  
inc cx
push dx  
mov dx,0
cmp ax,0
je s1  
jmp printNumber  

s1:
mov ah,2
pop dx
or dl,30h
int 21h
loop s1
ret


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

code ends
end start

运行测试截图

image-20211213220509793

 

4. 实验任务4

此部分书写内容:

task4.asm源码

assume cs:code, ds:data
data segment
str db "assembly language, it's not difficult but tedious"
len equ $ - str
data ends

code segment
start:  
mov ax, data
mov ds, ax
mov si, offset str
mov cx, len

s: call strupr

mov ah, 4ch
int 21h

strupr:  
s1: mov al, [si]
cmp al, 'a'
jb s2
cmp al, 'z'
ja s2
sub byte ptr [si], 32
s2: inc si
loop s1

code ends
end start

在debug中调试截图( call strupr 调用之前,数据段的值,以及,调用之后,数据段的值)

image-20211213220945454

 

5. 实验任务5

此部分书写内容:

task5.asm源码

assume cs:code, ds:data

data segment
   str1 db "yes", '$'
   str2 db "no", '$'
data ends

code segment
start:
   mov ax, data
   mov ds, ax

   mov ah, 1
   int 21h

   mov ah, 2
   mov bh, 0
   mov dh, 24
   mov dl, 70
   int 10h

   cmp al, '7'
   je s1
   mov ah, 9
   mov dx, offset str2
   int 21h

   jmp over

s1: mov ah, 9
   mov dx, offset str1
   int 21h
over:  
   mov ah, 4ch
   int 21h
code ends
end start

程序运行测试截图(输入7,以及输入其他字符,运行结果截图)

image-20211213221340686

task程序的功能是?

功能:从键盘输入一个字符,如果为'7',则在指定的24行,70列输出'yes';否则在指定位置输出'no'。

 

6. 实验任务6

此部分书写内容:

通过此项实现任务,你对中断、软中断实现机制的理解

自己选一个未被使用的中断码,实现一个中断例程,并调用测试。给出源码和运行测试截图。

如选做,请说明你使用的中断码,并描述你实现的这个中断例程的功能。

assume cs:code
code segment
start:
  ; 42 interrupt routine install code
  mov ax, cs
  mov ds, ax
  mov si, offset int42  ; set ds:si
  mov ax, 0
  mov es, ax
  mov di, 200h     ; set es:di
  mov cx, offset int42_end - offset int42
  cld
  rep movsb
  ; set IVT(Interrupt Vector Table)
  mov ax, 0
  mov es, ax
  mov word ptr es:[42*4], 200h
  mov word ptr es:[42*4+2], 0
  mov ah, 4ch
  int 21h
int42:
  jmp short int42_start
  str db "welcome to 2049!"
  len equ $ - str
  ; display string "welcome to 2049!"
int42_start:
  mov ax, cs
  mov ds, ax
  mov si, 202h
  mov ax, 0b800h
  mov es, ax
  mov di, 24*160 + 32*2
  mov cx, len
s: mov al, [si]
  mov es:[di], al
  mov byte ptr es:[di+1], 2
  inc si
  add di, 2
  loop s
  iret
int42_end:
 nop
code ends
end start

 

assume cs:code
code segment
start:
  int 42 ; 调用自己实现的42号软中断
  mov ah, 4ch
  int 21h
code ends
end start

image-20211213222212347

 

编写一个软中断指令的可以分为以下三步: 1.拷贝中断代码到系统空闲的内存处 2.在中断向量表中添加相应的中断代码的段地址和偏移量 3.编写中断代码

posted @ 2021-12-13 22:26  当我傻  阅读(17)  评论(0)    收藏  举报