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

实验任务1

task4_1.asm源码:

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

回答问题:

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

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

 

调试截图:

 

 

实验任务2:

task4_2.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

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

答:从键盘上读取输入的字符,并保存到ds:[si],每读入一个就判断是否为#,如果是则不保存,转跳至标号next处执行;如果不是则si + 1并继续读入下一个字符。

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

答:打印一个回车(换行符)。

③ 汇编指令代码line24-30,实现的功能是?

答:打印字符串,并且不会把#打出来。

运行截图:

 

 

 

 实验任务3:

task4_3.asm源码:

assume ds:data, cs:code, ss:stack

data segment
    x dw 91, 792, 8536, 65535, 2021, 0
    len equ $ - x
data ends

stack segment
    dw 8 dup(?)
stack ends

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

    mov ax, stack
    mov ss, ax
    mov sp, 16

    mov cx, len/2
    print:
        mov ax, word ptr ds:[di]
        add di, 2

        push cx
        call printNumber
        call printSpace

        pop cx
    loop print

    mov ah, 4ch
    int 21h

; 子程序: printNumber
; 功能: 打印数字
;   入口参数:
;       寄存器ax  (待输出的数据 --> ax)
;   局部变量说明:
;       bx -> 存储数字字符个数
printNumber:
    mov bx, 0
    ; getEach循环: 获取每一位,然后压入栈中
    getEach:
        ; 除数放在16位寄存器bp中
        mov bp, 10
        mov dx, 0
        div bp

        push dx
        inc bx

        mov cx, ax
        inc cx

    loop getEach

    ; 打印数字
    mov cx, bx
    printEach:
        pop dx
        add dl, 30h
        mov ah, 2
        int 21h
    loop printEach

    ret

; 子程序: printSpace
; 功能: 打印空格
printSpace:
    mov ah, 2
    mov dl, 20h
    int 21h
    ret

code ends
end start

运行截图:

 

 

 

 

 

实验任务4:

task4_4.asm源码:

assume ds:data,cs:code
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, 0       ;数据段指针
    mov cx, len     ;字符串长度
    call strupr     ;调用小写转大写子程序
    jmp exit        ;退出程序

strupr:             
s:
    mov al, [si]    ;当前字符放入al
    cmp al, 61h     ;ASCII码在[61h,7ah]为小写字母
    jb s1
    cmp al, 7ah
    ja s1
    and al, 0dfh    ;二进制位第三位变为0,小写转大写
    mov [si], al
s1:
    inc si          ;下个字符
    loop s
    ret

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

子程序调用前:

 

 

 子程序调用后:

 

 

 可以看到字符串中的小写字母成功转成大写字母。

 

 

 

task4_5.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:

 

 

 输入其他字符:

 

 

 

功能:从键盘键入一个字符,如果是7,在光标位置(24行70列)打印str1;是其他字符,在光标位置打印str2。

分析:

line13-14 键盘键入一个字符,存入al

line16-20 设置光标位置,24行70列

line22-26 al中字符不为7,在光标位置打印str2

line30-32 al中字符为7,在光标位置打印str1

 

实验任务6:

task4_61.asm源码:

;功能:装入42号程序中断处理程序
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

task4_62.asm源码:

assume cs:code

code segment
start:
    int 42

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

调试运行:

 

 

 

对中断、软中断的理解

中断: 中断是指由于接收到外围硬件(相对于CPU与内存而言)的异步信号或者来自软件的同步信号而进行相应的硬件/软件处理 ;

软中断: 由软件本身发给操作系统内核的中断信号,称之为软中断 ;

中断处理:

(1)(从中断信息中)取得中断类型码;

(2)标志寄存器的值入栈(在中断过程中要改变标志寄存器的值);

(3)设置标志寄存器的第8位TF和第9位IF的值为0;

(4)CS、IP的内容依次入栈;

(5)从内存地址为中断类型码*4和中断类型码*4+的两个字单元中读取中断处理程序的入口地址设置IP和CS。

 

自定义中断程序:41号中断码

中断例程功能:

使用的中断码为41号,中断例程功能为在屏幕中间显示“201983290219 wanglicong”。

task4_63.asm源码:

;装入中断处理程序
assume cs:code
code segment
start:
    ;ds:si指向中断处理程序
    mov ax, cs
    mov ds, ax
    mov si, offset int41

    ;es:di指向程序装入地址
    mov ax, 0
    mov es, ax
    mov di, 200h

    ;获取程序长度
    mov cx, offset int41end-int41
    cld
    rep movsb

    ;设置中断向量表
    mov ax, 0
    mov es, ax
    mov word ptr es:[41*4], 200h
    mov word ptr es:[41*4+2], 0
    
    mov ah, 4ch
    int 21h

;41号中断处理程序
int41:
    jmp short int41start
    db "201983290219 wanglicong"        ;存放要打印的字符串
int41start:
    ;ds:si指向待打印字符串
    mov ax, cs
    mov ds, ax
    mov si, 202h

    ;es:di指向屏幕中间
    mov ax, 0b800h
    mov es, ax
    mov di, 12*160+30*2

    ;cx字符串长度
    mov cx, 13h
s:  mov al, [si]
    mov es:[di], al                 
    mov byte ptr es:[di+1], 2       ;黑底绿字
    inc si
    add di, 2
    loop s

    mov ax, 4c00h
    int 21h

int41end:nop
code ends
end start

task4_64.asm源码:

assume cs:code

code segment
start:
    int 41

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

调试运行:

 

posted @ 2021-12-13 21:35  思念化为君  阅读(61)  评论(2编辑  收藏  举报