"Assembly Language" - Reading Notes - Chapter 5 - [BX] and loop instructions
- 5.1 [BX]
- 5.2 Loop instruction
- 5.3 Track the loop program implemented with the loop instruction in Debug
- 5.4 Different handling of instructions by Debug and assembler masm
- 5.5 Joint application of loop and [bx]
- 5.6 Segment prefix
- 5.7 A Safe Space
- 5.8 Use of segment prefixes
- Experiment 4 [bx] and the use of loop
- [bx] and a description of the memory unit
instruction | segment address | offset address | illustrate |
---|---|---|---|
mov ax, [0] |
ds | 0 | Send the contents of the unit at ds:0 memory to ax字 |
mov al, [0] |
ds | 0 | Send the contents of the unit ds:0 at memory to the lower 8 bits of ax字节 |
mov ax, [bx] |
ds | value in bx | Send the contents of the unit at ds:[bx] memory to ax字 |
mov al, [bx] |
ds | value in bx | Send the contents of the unit at the memory ds:[bx] location to the lower 8 bits of ax字节 |
-
loop
The loop directive represents a loop. -
Defined descriptive symbols: "()"
Convention in the book:()
Brackets are used to indicate the content in寄存器
or内存
.
()
You can enter: register name, segment register name, physical address of memory unit.
(ax) indicates the content in ax;
(al) indicates the content in al;
(20000H) indicates the content of memory unit 20000H ( ()
the address of the memory unit in is the physical address);
((ds)*16+(bx)) Indicates: the inner layer ()
fetches the value from ds and bx, and uses a physical address after calculating the result, and the outer layer ()
fetches the content from the memory address.
ax
The content in is 0010H , which can be described as follows: (ax)=0010H ;2000:1000
The content at the place is 0010H , which can be described as follows: (21000H)=0010H ;- For
mov ax,[2]
the function of , it can be described as follows: (ax)=((ds)*16+2) ; - For
mov [2],ax
the function of , it can be described as follows: ((ds)*16+2)=(ax) ; - For
add ax,2
the function of , it can be described as follows: (ax)=(ax)+2 ; - For
add ax,bx
the function of , it can be described as follows: (ax)=(ax)+(bx) ; - For
push ax
the function of , it can be described as follows:
(sp)=(sp)-2
((ss)*16+(sp))=(ax)
- For the function of pop ax, it can be described as follows:
(ax)=((ss)*16+(sp))
(sp)=(sp)+2
- The convention symbol idata represents
the convention in the constant book: idata represents the constant.
The agreement in the book is convenient for communication, and it is not really a legal instruction.
mov ax,[idata]
on behalf of mov ax,[1]
, mov ax,[2]
, mov ax,[3]
etc.
mov bx, idata
on behalf of mov bx,1
, mov bx,2
, mov bx,3
etc.
mov ds, idata
on behalf of mov ds,1
, mov ds,2
etc.
5.1 [BX]
mov ax, [bx]
Function: The data stored in bx is used as an offset address EA, the segment address SA is defaulted in ds, and the data at SA:EA is sent to ax.
That is: (ax)=((ds)*16+(bx)).
mov[bx], ax
Function: The data stored in bx is used as an offset address EA, the segment address SA is defaulted in ds, and the data in ax is sent to the memory SA:EA. That is: ((ds)*16+(bx))=(ax).
Question 5.1
The situation in the program and memory is shown in Figure 5.1. After the program is executed, write the content in the 21000H~21007H unit.
5.2 Loop instruction
Usually, loop is used to implement the loop function, and the number of loops is stored in cx. The format of the command is: loop 标号
.
When the CPU executes the loop instruction, there are two steps:
(cx)=(cx) - 1
;- If cx != 0 is judged: go to
标号
execute, otherwise: continue to execute downward.
task 1
Program to calculate 2^2, and the result is stored in ax.
task 2
Programming to calculate 2^3
mov ax, 2
add ax, ax
add ax, ax
task 3
Programming to calculate 2^12
mov ax, 2
add ax, ax ; 重复 11 次
Used to lead out the loop loop
Procedure 5.1
assume cs:abc
abc segment
mov ax, 2
mov cx, 11 ; 循环 11 次
s: add ax, ax
loop s ; cx-1 后如果不为 0,转到 s 处执行
mov ax, 4c00H
int 21H
abc ends
end
Initially 2 and then add ax, ax
11 times to achieve2^12
Question 5.2
Programming, use addition to calculate 123 * 236 , and store the result in ax.
Analysis: It can be completed with a loop, 123 * 236 is equal to 123 added 236 times.
assume cs:abc
abc segment
mov ax, 123
mov cx, 236
s: add ax, 123
loop s
mov ax, 4c00H
int 21H
abc ends
end
Question 5.2
Improve program 5.2 to increase the calculation speed of 123*236 .
Analysis: 123 * 236 is equal to 123 added 236 times. It can also be 236 added 123 times.
assume cs:abc
abc segment
mov ax, 236
mov cx, 123
s: add ax, 236
loop s
mov ax, 4c00H
int 21H
abc ends
end
5.3 Track the loop program implemented with the loop instruction in Debug
The usage of t, p, and g in debug will not be repeated. For details, see: "Assembly Language" - Reading Notes - Experiment 1 Check the CPU and memory, and program with machine instructions and assembly instructions
5.4 Different handling of instructions by Debug and assembler masm
Debug
instruction | illustrate |
---|---|
mov ax, [0] | ds:0 Send the contents of memory toax |
MASM
instruction | illustrate |
---|---|
mov al, [0] | (al)=0 , send the constant 0 into al (same meaning as mov al,0); |
mov al, ds:[0] | (al)=((ds)*16+0) , send the memory cell into 数据 the al ; |
mov al, [bx] | (al)=((ds)*16+(bx)) , send the memory cell into 数据 the al ; |
mov al,ds:[bx] | equivalentmov al, [bx] |
Summary: In masm, if the offset is represented by a constant, the address of the specified segment should be displayed.
5.5 Joint application of loop and [bx]
Computes ffff:0~ffff:B
the sum of the data in cells and stores the result in dx
.
analyze:
dx
Can you let it go?
Answer: There are at most 12 FFs from 0 to b = 11EE, and dx has a two-byte maximum storage of FFFF, so the space is no problem.ffff:0~ffff:B
Can the number of be added directly indx
?
Answer: No. Becauseffff:0~ffff:B
in is a byte.dx
It is a word (occupies two bytes.) Because another register is needed for temporary transfer.- Can it be accumulated directly
dl
?
Of course not. Because the cumulative result exceedsFF
, it will onlydl
overflow.
Use temporary workersax
. For example (al) = (ffff*16+0), (ah) = 0. put字节
in字
, the value remains unchanged.
Procedure 5.5
assume cs:code
code segment
mov ax, 0ffffh ;数值不能以字母开头,故前面加 0
mov ds, ax ;设置(ds)=ffffh
mov dx, 0 ;初始化累加寄存器,(dx)=0
mov al, ds:[0] ;从内存取值,给临时工 ax。
mov ah, 0 ;清空高位
add dx, ax ;执行累加
; 累计操作共重复12次。
mov ax, 4c00h
int 21h
code ends
end
Question 5.4
There is a problem with the code of program 5.5, which is repeated many times, and the loop should be used to realize the loop.
Procedure 5.6
assume cs:code
code segment
mov ax, 0ffffh ;数值不能以字母开头,故前面加 0
mov ds, ax ;设置(ds)=ffffh
mov bx, 0 ;bx中存偏移地址,初始为 0
mov dx, 0 ;初始化累加寄存器,(dx)=0
mov cx, 12 ;设置循环次数
s: mov al, [bx] ;从内存取值,给临时工 ax
mov ah, 0 ;清空高位
add dx, ax ;执行累加
inc bx ;bx+1让偏移地址指向下一个字节
loop s ;检测循环条件,符合就循环,否则向下继续
mov ax, 4c00h
int 21h
code ends
end
5.6 Segment prefix
mov ax, ds:[bx]
mov ax, cs:[bx]
mov ax, ss:[bx]
mov ax, es:[bx]
mov ax, ss:[0]
mov ax, cs:[0]
段地址
These appear in the instructions to access the memory unit, and the ds:, cs:, ss, and es used to explicitly specify the memory unit are called in assembly language 段前缀
.
5.7 A Safe Space
In general PC, in DOS mode, DOS and other legitimate programs will generally not use 0:200~0:2ff
the 256-byte space (00200h~002ffh).
For the sake of caution, after entering DOS, we can use Debug to check it first. If 0:200~0:2ff
the contents of the unit are 0
all , it proves that no one is using this place.
5.8 Use of segment prefixes
Procedure 5.8
Book first wrote an unreasonable version that changes segment prefixes in a loop. lead to something later.
Procedure 5.9
This time, two segment registers are used to break the defense source and target respectively. It saves the trouble of messing with segment registers in the loop.
assume cs:code
code segment
mov ax, 0ffffh
mov ds, ax ; 设置源段前缀
mov ax, 0020h
mov es, ax ; 设置目标源段前缀
mov bx, 0 ; 偏移量从0开始(源和目标皆从0开始,正好共用)
mov cx, 12 ; 循环12次
s:mov dl, ds:[bx] ; 从源内存取值,送入dl
mov es:[bx], dl ; 从 dl 取值送入目标内存
inc bx ; 偏移地址向前移动
loop s ; 判断cx不为0就循环,否则继续向下。
mov ax, 4c00h
int 21h
code ends
end
Experiment 4 [bx] and the use of loop
"Assembly Language" - Reading Notes - Experiment 4 [bx] and the use of loop