代码改变世界

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

2021-11-30 14:07  kirimi  阅读(21)  评论(5编辑  收藏  举报

任务一

  • task1.asm以及运行截图
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

  • 问题①

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


从001B跳转到000D,偏移量为-14,偏移地址为-14的补码F2

  • 问题②

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


从0039跳转到0029,偏移量为-16,偏移地址为-14的补码F0

任务二

  • 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
  • 给出分析、调试、验证后,寄存器(ax) = ? (bx) = ? (cx) = ? 附上调试结果界面截图。

    ax= 0021
    bx= 0026
    cx= 0772
    call word将IP地址入栈,出栈时将IP存入地址赋给ax

    call dword将CS:IP入栈,出栈时先付给bx IP地址,再赋给cx CS地址

任务三

  • task3.asm
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 si,offset x
	mov cx,len
 
s:	mov ah,0
	mov al,[si]
	call printNumber
	call printSpace
	inc si
	loop s	
	mov ah,4ch
	int 21h
printNumber:
	mov bl,10 
	div bl
	mov bx,ax
	mov ah,2
	mov dl,bl
	or dl,30h
	int 21h
	mov dl,bh
	or dl,30h
	int 21h
	ret
	
printSpace:
	mov ah,2
	mov dl,' '
	int 21h
	ret
		
code ends
end start
  • 运行

任务四

在我的电脑上str db 'try'会出现当前CPU模式不认可的指令或寄存器错误,下面改为string

  • task4.asm
assume cs:code, ds:data

data segment 
    string db 'try'
    len equ $ - string
data ends
code segment 
start:
	mov ax,data
	mov ds,ax
	mov cx,len
	mov si,offset string
	mov bh,0
	mov bl,2
	call printStr
	mov cx,len
	mov si,offset string
	mov bh,24
	mov bl,4
	call printStr
	mov ah,4ch
	int 21h
printStr:
	mov al,0ah
	mul bh
	add ax,0b800h
	mov es,ax
	mov di,si
s:	mov al,ds:[si]
   	mov ah,bl
   	mov es:[di],ax
   	inc si
  	add di,2
   	loop s
   	ret
code ends
end start
  • 运行

任务五

  • task5.asm
assume  cs:code,ds:data
data segment
	stu_no db '20498329042'
	len = $ - stu_no
data ends
code segment
start:  
    mov ax, data
    mov ds, ax
	mov ax,0B800H
	mov es,ax

	mov cx,0780H
	mov ah,10H
	mov al,' '
	mov bx,0
s:  mov es:[bx],ax
	add bx,2
	loop s	
	mov cx,80
	mov ah,17H
	mov al,'-'
s1: mov es:[bx],ax
	add bx,2
	loop s1
	mov cx,len
	mov bx,0F44H
	mov si,0
s2: mov al,[si]
	mov es:[bx],ax
	inc si
	add bx,2
	loop s2

	mov ah, 4ch
    int 21h

code ends
end start

  • 运行