当前位置:网站首页>Assembly language learning (2)
Assembly language learning (2)
2022-08-09 14:50:00 【The goal is a tech house】
I learned about Wang Shuang today《汇编语言》第5、6章.
I feel that these two chapters mainly deepen the for段这个概念的理解.
第5章[Bx]和loop指令,重点在于Understanding of segment addresses and offset addresses,段前缀的应用.
第6Chapters contain programs with multiple segments,重点在于Understand why segmenting is required,Learn how to write segmented assembler,Figure out how segments are stored in memory.
1.loop指令和[bx]的应用
1.1 计算ffff:0~ffff:bThe sum of the data in the cell,并将结果保存在dx中?
存在的问题:8The accumulated sum of the memory cells of the bits is placed16位的寄存器中.
解决的方法:以ax为中介,其中ah设为0,alis the value of the memory unit.
mov ax,0ffffh里面,0和h一个都不能少.
assume cs:code
code segment
mov ax,0ffffh
mov ds,ax
mov ax,0
mov dx,0
mov bx,0
mov cx,12
s:mov al,[bx]
add dx,ax
inc bx
loop s
mov ax,4c00h
int 21h
code ends
end1.2 将内存ffff:0~ffff:b单元中的数据复制到0:200~0:20b中?
段前缀的应用,ds、cs、ss、es都可以使用,很灵活.
注意题目是,将内存ffff:0~ffff:b单元中的数据复制到0:200~0:20b单元中.Set the segment prefix to 0020h比0要方便.
mov ax,0ffffh
mov ds,ax
mov ax,0020h
mov es,ax
mov bx,0
mov cx,12
s: mov dl,[bx]
mov es:[bx],dl
inc bx
loop s2.包含多个段的程序
2.1 How to accumulate data?
That is, how some data constants should be declared,如何引用?
采用dw 0123h,0456h,0789h进行声明.
通过cs:[bx]进行定位,bx每次增加2个字节.
But just make these changes,Problems arise at runtime.Take a look at the program's memory first:

可以看出前6bytes store the value of three words,To avoid treating data as instructions,程序应该从0C53:0006开始执行.
So it needs to be used in the source programend标号,The entry address of the marked program.
2.2 利用栈,Store the data defined in the program in reverse?
首先,Declare a space ahead of time,for stack use.
另外,Set the segment address and offset address of the stack.
示例程序如下:
assume cs:code
code segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
dd 0,0,0,0,0,0,0,0
start:mov ax,cs
mov ss,ax
mov sp,30h
mov bx,0
mov cx,8
s: push cs:[bx]
add bx,2
loop s
mov bx,0
mov cx,8
s1:pop cs:[bx]
add bx,2
loop s1
mov ax,4c00h
int 21h
code ends
end start2.3 结构更清晰——Divide the code into multiple segments
Below is the example mentioned earlier,Don't overstate the data、Stack and code three segments.
需要注意的是:
1.在assume之后,CPU不会将CS指向code,不会将DS指向data,不会将SS指向stack.
2.endIndicates the entry of the program,这个入口将被写入可执行文件的描述信息.After the program in the executable is loaded into memory,CPU的CS:IP被设置指向这个入口,Start executing the first instruction in the program.
所以,CSThe value does not need to be set in the code,但是DS和SSThe value of needs to be explained in the code.
3.对于一个段,如果段中的数据占N个字节,则程序加载后,该段实际占有的空间为:
(N/256+1)*256
where division rounds up.
assume cs:code,ds:data,ss:stack
data segment
dw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h
data ends
stack segment
dw 0,0,0,0,0,0,0,0
stack ends
code segment
start: mov ax,stack
mov ss,ax
mov sp,20h
mov ax,data
mov ds,ax
mov bx,0
mov cx,8
s: push [bx]
add bx,2
loop s
mov bx,0
mov cx,8
s1: pop [bx]
add bx,2
loop s1
mov ax,4c00h
int 21h
code ends
end startDebug by yourself,Discover how segments are stored in memory,It's an interesting thing.
But the more interesting things are always behind(手动滑稽)
边栏推荐
猜你喜欢
随机推荐
汇编语言学习(二)
Firewalld防火墙基础
C语言 求一个整数存储在内存中的二进制中1的个数(多种方法详解)
Minesweeper game
RHCE课程总结
RHCE课程总结
*4-2 CCF 2014-12-2 Z字形扫描
markdown学习1
#25-1 OJ 78 计算生日星期几
使用 compose 的 Canvas 自定义绘制实现 LCD 显示数字效果
扫雷小游戏
现实版商战:“武功再高,也怕菜刀”
汇编语言学习(三)
C语言,if循环 for 循环 while循环 switch循环 do...while()循环
递归实现汉诺塔问题
青蛙跳台阶
iptables防火墙
*1-5 OJ 642 Russian Multiplication
蓝桥杯嵌入式(STM32F103RBT6)最全攻略(一)
Es7.x使用RestHighLevelClient进行查询操作








