实验4

时间:2020-12-18 12:10:09   收藏:0   阅读:3
  1. 实验任务1

编程:在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串‘welcome to masm!‘。

在内存地址空间中,B8000H~BFFFFH供32KB的空间,为80×25彩色字符模式的显示缓冲区。
25行,居中的三行为12、13、14行,对应起始地址1760、1920、2080;需要显示的字符共16个,一行80列,即从32列开始,对应到地址应该+64,即1824、1984、2144。
而每个字符占两个字节:低位存放字符的ASCII码值,高位存放字符的显示属性,且顺序为:闪烁 背景色(RGB) 高亮 前景色(RGB)
根据属性字节的格式即可按位设置属性字节
黑底绿字:属性字节为00000010B,十六进制为02H;
绿底红字:属性字节为00100100B,十六进制为24H;
白底蓝字:属性字节为01110001B,十六进制为71H。

这里我直接循环3次,分别输出三行,虽然多重循环能够减少代码量,但命令条数基本一致,3次循环逻辑更加简单。
源程序:

assume cs:code, ds:data
data segment
	     db ‘welcome to masm!‘
	     db 00000010B,00100100B,01110001B
data ends

code segment
	start:
	      mov  ax, data
	      mov  ds, ax       	;将ds设为data段

	      mov  ax, 0b800H
	      mov  es, ax       	;将es段设为0b800h,为显存段

	      mov  cx, 16       	;共16个字符循环16次
	      mov  si, 0
	      mov  di, 1824     	;1760+64 第11列
	s1:   mov  al, [si]     	;ds:0(N)存入al,即data段的字节依次存入al
	      mov  es:[di], al  	;设置显示的字符
	      mov  al, ds:[16]
	      mov  es:[di+1], al	;设置显示字符的颜色
	      inc  si           	;si+1,指向下一个字符
	      add  di, 2        	;di+2,指向下一个显存字
	      loop s1

	      mov  cx, 16
	      mov  si, 0
	      mov  di, 1984     	;1920+64 第13列
	s2:   mov  al, [si]
	      mov  es:[di], al
	      mov  al, ds:[17]
	      mov  es:[di+1], al
	      inc  si
	      add  di, 2
	      loop s2

	      mov  cx, 16
	      mov  si, 0
	      mov  di, 2144     	;2080+64 第14列
	s3:   mov  al, [si]
	      mov  es:[di], al
	      mov  al, ds:[18]
	      mov  es:[di+1], al
	      inc  si
	      add  di, 2
	      loop s3

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

运行结果截图
技术图片

  1. 实验任务2
    此部分书写内容:
    附上源程序
assume cs:code, ds:data
data segment
	str  db ‘try‘, 0
data ends

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

	         mov  si, offset str
	         mov  al, 2
	         call printStr

	         mov  ah, 4ch
	         int  21h

	printStr:
	         push bx
	         push cx
	         push si
	         push di

	         mov  bx, 0b800H
	         mov  es, bx
	         mov  di, 0
	s:       mov  cl, [si]
	         mov  ch, 0
	         jcxz over
	         mov  ch, al
	         mov  es:[di], cx
	         inc  si
	         add  di, 2
	         jmp  s

	over:    pop  di
	         pop  si
	         pop  cx
	         pop  bx
	         ret

code ends
end start

运行结果截图:
技术图片
将line3改为str db ‘another try‘, 0实际上是更改输出串
将line12改为mov al, 4,实际上是更改字符串颜色
运行结果截图:
技术图片

line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?

将进入子程序前的寄存器通过栈保存起来,保留现场,在子程序结束后还原现场,让程序能够继续执行

line30的功能是什么?

设置字符的颜色属性

  1. 实验任务3
    此部分书写内容:
assume cs:code, ds:data
data segment
	x    dw 1984
	str  db 16 dup(0)
data ends

code segment
	start:  
	        mov  ax, data
	        mov  ds, ax
	        mov  ax, x
	        mov  di, offset str
	        call num2str

	        mov  ah, 4ch
	        int  21h

	num2str:
	        push ax
	        push bx
	        push cx
	        push dx
        
	        mov  cx, 0
	        mov  bl, 10
	s1:     
	        div  bl
	        inc  cx
	        mov  dl, ah
	        push dx
	        mov  ah, 0
	        cmp  al, 0
	        jne  s1
	s2:     
	        pop  dx
	        or   dl, 30h
	        mov  [di], dl
	        inc  di
	        loop s2
        
	        pop  dx
	        pop  cx
	        pop  bx
	        pop  ax

	        ret
code ends
end start

由于程序过长超过了显示范围,因此只反汇编默认长度
技术图片
可以看到1984写入成功
技术图片
附上子任务2修改、完善后的完整汇编源代码,及,运行测试截图

assume cs:code, ds:data
data segment
	x    dw 1984
	str  db 16 dup(0)
data ends

code segment
	start:   
	         mov  ax, data
	         mov  ds, ax
	         mov  ax, x
	         mov  di, offset str
	         call num2str
	         mov  si,offset str 	;指定字符串起始位置
	         mov  al, 4         	;指定颜色(绿色)
	         call printStr
	         mov  ah, 4ch
	         int  21h

	num2str: 
	         push ax
	         push bx
	         push cx
	         push dx
        
	         mov  cx, 0
	         mov  bl, 10
	s1:      
	         div  bl
	         inc  cx
	         mov  dl, ah
	         push dx
	         mov  ah, 0
	         cmp  al, 0
	         jne  s1
	s2:      
	         pop  dx
	         or   dl, 30h
	         mov  [di], dl
	         inc  di
	         loop s2
        
	         pop  dx
	         pop  cx
	         pop  bx
	         pop  ax

	         ret

	printStr:
	         push bx
	         push cx
	         push si
	         push di

	         mov  bx, 0b800H
	         mov  es, bx
	         mov  di, 0
	s:       mov  cl, [si]
	         mov  ch, 0
	         jcxz over
	         mov  ch, al
	         mov  es:[di], cx
	         inc  si
	         add  di, 2
	         jmp  s

	over:    pop  di
	         pop  si
	         pop  cx
	         pop  bx
	         ret
code ends
end start

技术图片
可以看到输出成功
4. 实验任务4
此部分书写内容:
程序源代码
运行测试截图
回答任务中提出的两个问题
5. 实验任务5
此部分书写内容:
总结对这个简单的c代码反汇编后,你对反汇编出来的汇编指令分析的内容总结。可以包括但不限于一
下内容的总结:
比如,高级语言中参数传递,从汇编的角度是如何传递的,返回值是如何返回的;多个参数的入栈顺序
是什么样的;函数调用栈,等等。
五、实验总结
此部分书写内容:(可以包括但不限以下内容)
通过本次实验你收获的具体知识点、思考等的归纳和梳理(可借助思维导图或大纲形文档结构)
通过本次实验你的新发现、体会/感受;尚存的问题
其它你愿意反馈、分享的内容

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!