"Assembly Language" - Reading Notes - Chapter 5 - [BX] and loop instructions

  1. [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:0memory to ax
mov al, [0] ds 0 Send the contents of the unit ds:0at 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字节
  1. loop
    The loop directive represents a loop.

  2. 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.

  1. axThe content in is 0010H , which can be described as follows: (ax)=0010H ;
  2. 2000:1000The content at the place is 0010H , which can be described as follows: (21000H)=0010H ;
  3. For mov ax,[2]the function of , it can be described as follows: (ax)=((ds)*16+2) ;
  4. For mov [2],axthe function of , it can be described as follows: ((ds)*16+2)=(ax) ;
  5. For add ax,2the function of , it can be described as follows: (ax)=(ax)+2 ;
  6. For add ax,bxthe function of , it can be described as follows: (ax)=(ax)+(bx) ;
  7. For push axthe function of , it can be described as follows:
(sp)=(sp)-2
((ss)*16+(sp))=(ax)
  1. For the function of pop ax, it can be described as follows:
(ax)=((ss)*16+(sp))
(sp)=(sp)+2
  1. 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, idataon behalf of mov bx,1, mov bx,2, mov bx,3etc.
mov ds, idataon behalf of mov ds,1, mov ds,2etc.

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.
insert image description here

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:

  1. (cx)=(cx) - 1;
  2. 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.
insert image description here

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, ax11 times to achieve2^12
insert image description here

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:0Send the contents of memory toax

MASM

instruction illustrate
mov al, [0] (al)=0, send the constant 0into 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:Bthe sum of the data in cells and stores the result in dx.
analyze:

  1. dxCan 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.
  2. ffff:0~ffff:BCan the number of be added directly in dx?
    Answer: No. Because ffff:0~ffff:Bin is a byte. dxIt is a word (occupies two bytes.) Because another register is needed for temporary transfer.
  3. Can it be accumulated directly dl?
    Of course not. Because the cumulative result exceeds FF, it will only dloverflow.
    Use temporary workers ax. 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

insert image description here

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:2ffthe 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:2ffthe contents of the unit are 0all , 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

Guess you like

Origin blog.csdn.net/jx520/article/details/130848963