汇编8086程序
我是汇编语言的新手,这是一些我不理解的代码,希望有人能帮忙。
DATA SEGMENT
VALUES DB 1,2,3,4,5,6,7,8,9
ITEM DB 6
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DX,AX
LEA SI,VALUES
MOV AL,ITEM
COMPARE: CMP AL,[SI]
JZ Found
INC SI
LOOP COMPARE
CLC
JMP EXIT
Found: STC
EXIT: MOV AH,4CH
INT 21H
ENDS
CODE END
该程序应该在 1,2,3,4,5,6,7,8,9
中查找数字 (6)
我了解它的一般工作原理但我有几个问题:
- 为什么我们使用
CLC
和STC
?我知道他们将CF
放入零和一,但我们为什么要使用它? - 为什么我们在
EXIT
标签后面使用MOV AH,4CH
? 在开始标签之后,为什么我们要说以下内容:
MOV AX,数据
MOV DX,AX
为什么我们不直接说: MOV DX,DATA
最后,有人可以推荐一本学习汇编的好书吗?
I am new to assembly language and this is some code that I didn't understand hoping that someone would help with it.
DATA SEGMENT
VALUES DB 1,2,3,4,5,6,7,8,9
ITEM DB 6
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DX,AX
LEA SI,VALUES
MOV AL,ITEM
COMPARE: CMP AL,[SI]
JZ Found
INC SI
LOOP COMPARE
CLC
JMP EXIT
Found: STC
EXIT: MOV AH,4CH
INT 21H
ENDS
CODE END
This program is supposed to look for number (6)
among 1,2,3,4,5,6,7,8,9
I understand how it works in general but I have a few questions:
- Why did we use
CLC
andSTC
?? I know they putCF
into zero and one but why do we use it?? - Why did we use
MOV AH,4CH
in after theEXIT
label?? After the start label why did we say the following:
MOV AX,DATA
MOV DX,AX
Why didn't we just say:MOV DX,DATA
Lastly, Could someone suggest a good book to learn assembly??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是错误的。
正确答案是:
我们不能直接将数据从内存发送到段寄存器(DS)。所以我们通过通用寄存器(AX)发送。
This is wrong.
Correct answer is:
we cant send data from memory to segment registers(DS) directly . so we are sending through General purpose registers(AX).
该算法在数字列表中搜索数字。
如果找到,则设置
CF
。如果没有找到,CF
将被清除。INT 21H 是 MS-DOS 服务中断。功能 4Ch 以 AL 中的错误代码(包含要查找的数字)结束程序。
The algorithm searches a number in a list of numbers.
If it is found,
CF
is set. If it is not foundCF
is cleared.INT 21H is the MS-DOS service interupt. Function 4Ch ends the program with an error code in AL (which contains the number to be found).
LOOP 指令在这里很奇怪。仅当
CX
不为零时,该指令才会递减CX
并跳转。这意味着循环运行CX
次,但程序永远不会设置CX
。CX
在输入时可能为零,第一次递减将使其变为 65535,因此实际上最多会循环 65536 次,如果未找到该元素,则搜索越过列表末尾。要使其正确,请在循环开始之前添加
MOV CX, ITEM - VALUES
。由于ITEM
紧随VALUES
之后,减去它们的地址将得到列表中的字节(元素)数。通常会在列表末尾添加标签以使此类计算更加稳健。
The
LOOP
instruction is curious here. This instruction decrementsCX
and jumps only ifCX
is not zero. This means that the loop runsCX
times, butCX
is never set by the program.CX
is likely zero on entry, and the first decrement will make it 65535, so it will actually loop a maximum of 65536 times, searching past the end of the list if the element is not found.To make it correct, add
MOV CX, ITEM - VALUES
before the loop start. SinceITEM
comes right afterVALUES
, subtracting their addresses will give the number of bytes (elements) in the list.Often a label is added to the end of a list to make such calculations more robust.
是错误的。一定是:
我们将数据段的地址写入DS寄存器,以便cpu知道去哪个地址查找我们的数据。而且由于x86指令集的限制,我们无法编写
MOV DS,DATA
,即CPU中没有实现这样的功能。每当将段地址写入段寄存器时,您都必须使用 AX 作为媒介。is wrong. It must be:
We write the address of the data segment to the
DS
register so that cpu knows which address to go look for our data. And we can't writeMOV DS,DATA
because of x86 instruction set limitations, i.e there is not such a function implemented in CPU. You have to useAX
as a medium whenever writing segment addresses to segment registers.CLC
指令用于“清除进位标志”,STC
指令用于“设置进位标志”。这些是过程控制指令,用于通过设置/重置标志值来控制处理器操作。MOV AH,4CH
用于终止当前进程。通过将4CH
的十六进制值存储(moving=MOV)到AH
寄存器中。MOV AX,DATA
指令是加载ax中数据段起始地址的方式。然后通过使用MOV DS,AX
,数据段被初始化。CLC
instruction is used to "Clear Carry Flag" and theSTC
is used to "set the carry flag". These are process control instructions, used to control the processor action by setting/resetting the flag values.MOV AH,4CH
is used to terminate from the current process. By storing(moving=MOV) the hex value of4CH
intoAH
register.MOV AX,DATA
instruction is way of loading starting address of data segment in ax. then by usingMOV DS,AX
,data segment gets initialized.