汇编8086程序

发布于 2024-10-24 22:33:10 字数 844 浏览 0 评论 0原文

我是汇编语言的新手,这是一些我不理解的代码,希望有人能帮忙。

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)

我了解它的一般工作原理但我有几个问题:

  1. 为什么我们使用 CLCSTC ?我知道他们将 CF 放入零和一,但我们为什么要使用它?
  2. 为什么我们在 EXIT 标签后面使用 MOV AH,4CH
  3. 在开始标签之后,为什么我们要以下内容:

    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:

  1. Why did we use CLC and STC ?? I know they put CF into zero and one but why do we use it??
  2. Why did we use MOV AH,4CH in after the EXIT label??
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

作死小能手 2024-10-31 22:33:10
MOV AX,DATA

MOV DX,AX

这是错误的。

正确答案是:

MOV AX,DATA   

MOV DS,AX

我们不能直接将数据从内存发送到段寄存器(DS)。所以我们通过通用寄存器(AX)发送。

MOV AX,DATA

MOV DX,AX

This is wrong.

Correct answer is:

MOV AX,DATA   

MOV DS,AX

we cant send data from memory to segment registers(DS) directly . so we are sending through General purpose registers(AX).

歌入人心 2024-10-31 22:33:10

该算法在数字列表中搜索数字。

如果找到,则设置 CF。如果没有找到,CF 将被清除。

INT 21H 是 MS-DOS 服务中断。功能 4Ch 以 AL 中的错误代码(包含要查找的数字)结束程序。

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           ; DS:SI points to the VALUES structure
       MOV AL,ITEM
COMPARE: CMP AL,[SI]           ; Compare with number in list
         JZ Found              ; Jump to Found if equal
         INC SI                ; Try next
         LOOP COMPARE          ;
         CLC                   ; Clear CF (not found)
         JMP EXIT              ; Quit
Found: STC                     ; Set CF (found)
EXIT: MOV AH,4CH               ; End program with error code AL = 6.
      INT 21H
      ENDS
CODE END

The algorithm searches a number in a list of numbers.

If it is found, CF is set. If it is not found CF 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).

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           ; DS:SI points to the VALUES structure
       MOV AL,ITEM
COMPARE: CMP AL,[SI]           ; Compare with number in list
         JZ Found              ; Jump to Found if equal
         INC SI                ; Try next
         LOOP COMPARE          ;
         CLC                   ; Clear CF (not found)
         JMP EXIT              ; Quit
Found: STC                     ; Set CF (found)
EXIT: MOV AH,4CH               ; End program with error code AL = 6.
      INT 21H
      ENDS
CODE END
染火枫林 2024-10-31 22:33:10

LOOP 指令在这里很奇怪。仅当 CX 不为零时,该指令才会递减 CX 并跳转。这意味着循环运行 CX 次,但程序永远不会设置 CX

CX 在输入时可能为零,第一次递减将使其变为 65535,因此实际上最多会循环 65536 次,如果未找到该元素,则搜索越过列表末尾。

要使其正确,请在循环开始之前添加 MOV CX, ITEM - VALUES。由于 ITEM 紧随 VALUES 之后,减去它们的地址将得到列表中的字节(元素)数。

通常会在列表末尾添加标签以使此类计算更加稳健。

         VALUES     DB 1,2,3,4,5,6,7,8,9
         VALUES_END LABEL BYTE
         ; ...

         MOV CX, VALUES_END - VALUES
COMPARE: ; ...
         LOOP COMPARE

The LOOP instruction is curious here. This instruction decrements CX and jumps only if CX is not zero. This means that the loop runs CX times, but CX 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. Since ITEM comes right after VALUES, 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.

         VALUES     DB 1,2,3,4,5,6,7,8,9
         VALUES_END LABEL BYTE
         ; ...

         MOV CX, VALUES_END - VALUES
COMPARE: ; ...
         LOOP COMPARE
情场扛把子 2024-10-31 22:33:10
MOV AX,DATA
MOV DX,AX

是错误的。一定是:

MOV AX,DATA
MOV DS,AX

我们将数据段的地址写入DS寄存器,以便cpu知道去哪个地址查找我们的数据。而且由于x86指令集的限制,我们无法编写MOV DS,DATA,即CPU中没有实现这样的功能。每当将段地址写入段寄存器时,您都必须使用 AX 作为媒介。

MOV AX,DATA
MOV DX,AX

is wrong. It must be:

MOV AX,DATA
MOV DS,AX

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 write MOV DS,DATA because of x86 instruction set limitations, i.e there is not such a function implemented in CPU. You have to use AX as a medium whenever writing segment addresses to segment registers.

离线来电— 2024-10-31 22:33:10
  • CLC指令用于“清除进位标志”,STC指令用于“设置进位标志”。这些是过程控制指令,用于通过设置/重置标志值来控制处理器操作。
  • 在ALP中,我们只能将数据加载到段寄存器中,首先将其加载到通用寄存器中,然后我们必须将其从通用寄存器移动到段寄存器中,这就是语法...
  • MOV AH,4CH 用于终止当前进程。通过将4CH的十六进制值存储(moving=MOV)到AH寄存器中。
MOV AX,DATA 
MOV DS,AX 
  • MOV AX,DATA指令是加载ax中数据段起始地址的方式。然后通过使用MOV DS,AX,数据段被初始化。
  • The CLC instruction is used to "Clear Carry Flag" and the STC is used to "set the carry flag". These are process control instructions, used to control the processor action by setting/resetting the flag values.
  • In ALP, we can only load a data into a segment register by, first loading it into a general purpose register and then we have to move it from this general register to the segment register, that is how the syntax is...
  • MOV AH,4CH is used to terminate from the current process. By storing(moving=MOV) the hex value of 4CH into AH register.
MOV AX,DATA 
MOV DS,AX 
  • MOV AX,DATA instruction is way of loading starting address of data segment in ax. then by using MOV DS,AX ,data segment gets initialized.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文