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

实验任务1

  • task3_1源码
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
  • 运行截图

  • 问题1

位移量为14,当前ip为001B,cpu根据loop指令后的标号得知其偏移地址为000D,相减得到偏移地址的差,由目前的ip地址加上差值得到标号后程序对应的偏移地址

  • 问题2

位移量为10,分析同上

实验任务2

  • task3_2源码

    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=offset s1,bx=offset s2,cx=cs

执行 call word ptr ds:[0] 时,将目前的ip即 pop ax 的偏移地址入栈,然后执行s1: pop ax,将栈内s1后代码的偏移地址赋给ax,bx同理,最后将cs赋给cx

实验任务3

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 al,[si]
mov ah,0h
call printNumber
inc si
loop s

mov ax,4c00h
int 21h

printNumber:
mov bl,10
div bl
mov bl,al
mov bh,ah
mov ah,2
mov dl,bl
or dl,30h
int 21h
mov dl,bh
or dl,30h
int 21h
call printSpace
ret

printSpace:
mov dl,' '
int 21h
ret


  
code ends
end start

实验任务4

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  si,offset str
	         mov  cx,len

	         mov  bl,00000010B
	         mov  bh,0

	         call printStr

             mov  si,offset str
	         mov  cx,len

	         mov  bl,00000100B
	         mov  bh,24

            call printStr
             
	         mov  ax,4c00h
	         int  21h
	         
	printStr:
	         mov  ax,0b800h
	         mov  es,ax

	         mov  ax,00a0h
	         mul  bh
	         mov  bp,ax
             mov  di,si
	s:       mov  al,ds:[si]
	         mov  es:[di+bp],al
	         mov  es:[di+bp+1],bl
	         inc  si
             add  di,2

	         loop s

	         ret

code ends
end start

实验任务5

assume cs:code, ds:data
data segment
	stu_no db '201983290504'
	len    =  $ - stu_no
data ends

code segment
	start:

	      mov ax,data
	      mov ds,ax
	      mov si,offset stu_no
	      mov cx,len

          call printblue
          call printstu_no


          mov ax,4c00h
          int 21h

          printblue:
          push cx
          mov ax,0b800h
          mov es,ax
          mov bl,' '
          mov bh,00010000B
          mov cx,2000
          mov bp,0
          s:mov es:[bp],bl
          mov es:[bp+1],bh
          add bp,2
          loop s

          pop cx
          ret


          printstu_no:
          push cx
          mov ax,0b800h
          mov es,ax
          mov bl,'-'
          mov bh,00010111B
          mov di,0f00h
          mov cx,80
          mov bp,0
          d:mov es:[di+bp],bl
          mov es:[di+bp+1],bh
          add bp,2
          loop d
          

          mov di,0f44h
          pop cx
          mov bp,0
         
          f:mov bl,ds:[si]
          mov es:[di+bp],bl
          mov es:[di+bp+1],bh
          add bp,2
          inc si
          loop f

          ret


code ends
end start

posted on 2021-11-29 20:06  憖叇苝歏  阅读(23)  评论(1)    收藏  举报