Assembly language (Wang Shuang edition) learning chapter notes [BX] and loop instruction (1)

Foreword

Description [BX] and memory unit

[bx] What is it?
And [0] is somewhat similar, but [0] indicates a memory unit, which offset address is 0.

我们要完整地描述一个内存单元,需要两种信息:

 1. 内存单元的地址
 2. 内存单元的长度(类型)

我们用[0]表示一个内存单元时,0表示单元的偏移地址,段地址默认在ds中,
单元的长度(类型)可以由具体指令中的其他操作对象(比如说寄存器)指出,
如前边的AX,AL。
[bx]同样也表示一个内存单元,它的偏移地址在bx中,如下面的指令:
mov ax,[bx]
mov ax,[bl]

loop
English "loop" represents the cycle

Description of the symbol "()"
for brevity of description, the following course, we will use a descriptive symbol "()" to represent the contents of a register or a memory unit.

例:

 - ax中的内容为0010H,我们可以这样描述:(ax)=0010H;
 - 2000:1000处的内容为0010H,我们可以这样来描述:(21000H)=0010H;
 - 对于mov ax,[2]的功能,我们可以这样来描述:(ax)=((ds)*16+2)
 - 对于push ax的功能,我们可以这样来描述:
	(sp)=(sp)-2;((ss)*16+sp)=(ax)
 - 对于pop ax的功能,我们可以这样来描述:
	(ax)=((ss)*16+(sp))
	(sp)=(sp)+2

Conventional symbols represent constant idata
"[...]" represents 0 in memory with a constant address offset, expressed by idata constant.

例:
mov ax,[idata]就代表mov ax,[1]、mov ax,[2]等

5.1 [BX]

Instruction function:

mov ax,[bx]
功能:bx中存放的数据作为一个偏移地址EA,段地址SA默认在ds中,将SA:EA处的数据送入ax中。

Issue 5.1

Here Insert Picture Description

inc bx 表示bx=bx+1;
整个程序逻辑还是比较简单的

Here Insert Picture Description


5.2 Loop instruction

格式:loop 标号
CPU执行loop指令的时候,要进行两步操作:
 - (cx)=(cx)-1;
 - 判断cx中的值,不为零则转至标号处执行程序,如果为零则向下执行。

cx affect the value of the execution result of the instruction loop.
Usually we use loop instruction and circulatory function, cx stored cycles.

任务:编程计算2^12
程序代码:
assume cs:code
code segment
	mov ax,2
	mov cx,11
	s:add ax,ax
	loop s
	mov ax,4c00h
	int 21h
code ends
end
程序分析:
(1)标号
在汇编语言中,标号代表一个地址,此程序中有一个标号s。它实际上标识了一个地址,这个地址处有一条指令:add ax,ax
(2)loop s
CPU执行loop s的时候,要进行两步操作:

 - (cx)=(cx)-1;
 - 判断cx中的值,不为0则转折标号s所标识的地址处执行(这里的指令是"add ax,ax"),如果为0则执行下一条指令(这里的下一条指令是"mov ax,4c00h")
(3)以下三条指令
mov cx,11
s:add ax,ax
loop s
执行loop s时,首先要将(cx)减一,然后若(cx)不为0,刚向前转至s处执行add ax,ax。
所以,我们可以利用cx来控制add ax,ax的执行次数。

Use cx and loop realize loops summary

  • Cx stored in the number of cycles
  • loop instruction address to be identified by reference numerals in the previous
  • To perform the loop block, to write in numerals and the intermediate loop instructions
程序框架:
mov cx,循环次数
s:
循环执行的程序段
loop s

Note:

自己编写了一个程序调试了一下:
assume cs:codesg
codesg segment
	
		mov ax,2
		mov cx,11
	s:	add ax,ax
		mov cx,7
		loop s
		mov ax,4c00h
		int 21h
codesg ends
end
如果在循环体内改变cx的值,会直接影响到判断条件中的cx,如果循环体中mov cx>1,
则程序会陷入死循环。
也就是说,loop循环是跳到标号处,从标号处继续往下执行,并不是仅仅执行标号处一行语句!!!

Issue 5.3

编程,计算123*236,结果放在ax中
程序:
assume cs:codesg
codesg segment
	
		mov ax,0
		mov cx,236
	s:	add ax,123
		loop s
		mov ax,4c00h
		int 21h
codesg ends
end

Tips:mov ax,236 ;mov cx,123;会提高计算效率

5.3 tracking loop implemented with a program loop in the instruction Debug

Consider the question, is calculated ffff: 0006 Number of units multiplied by 3, stores the result in the dx. We analyze:
whether (1) the results of the operation would exceed the scope of dx can store?

ffff:0006单元中的数是一个字节型的数据,范围在0~255之间,
则用它和3相乘结果不会大于65535,可以在dx中存放下。

(2) we use to achieve cycle multiply accumulate, accumulate with which register?

我们将ffff:0006单元中的数赋给ax,用dx进行累加。先设(dx)=0,然后做3次
(dx)=(dx)+(ax)。

(3) ffff: 0006 unit is a byte unit, AX is a 16-bit register, the data length is not the same, how to assign?

注意,“赋值”是让ax中数据的值和ffff:0006单元中的数据的值相等。
8位数据01h和16位数据0001h的数据长度不一样,但它们的值是相等的。
赋值过程:
令(ah)=0,(al)=(fff6h)

Programming:

assume cs:codesg
codesg segment
		mov ax,0ffffh ;汇编源程序中,数据不能以字母开头
		mov ds,ax
		mov bx,6
		mov al,[bx] ;以上,设置(ah)=0,(al)=((ds)*16+bx)
		mov ah,0
		mov dx,0 ;累加寄存器清零
		
		mov cx,3 ;循环3次
	s:	add dx,ax
		loop s
		
		mov ax,4c00h
		int 21h
codesg ends
end

Debug the debug trace for more program cycles.
G can command and command P

-g 0014 表示直接跳到ip=0014h处,假设loop s的ip是0012h,则此时循环已经执行完毕。
-p 后台执行完循环,下一步命令是 loop s时使用-p即可运行完循环。

Here Insert Picture Description
Here Insert Picture Description

Published 107 original articles · won praise 68 · views 7770

Guess you like

Origin blog.csdn.net/weixin_43092232/article/details/105044820