我正在尝试编写一个 8086 程序来比较以十六进制编写的整数数组。我知道最高数字是0xFF,但这并不会导致它是最大值。(我也不确定我是否正确结束了程序。这是我的代码:
DATA SEGMENT ;declares the data segment
SIZEA DW 10
MAXV DW 0
INTARR DW 0x1,0x2,0x4,0xFF,0xFE,0xFA,0x7,0x9,0x8,0xFD
DATA ENDS ;ends the data segment
CODE SEGMENT ;declares the code segment
START: ;declares start label
MOV AX,DATA ;moves the data to the AX register
MOV DS,AX ;moves the date from the AX reg to DS
LEA SI,INTARR ;loads the address of INTARR to SI
MOV BX,INTARR[SI] ;loads the first array item into BX
MOV CX, SIZEA ;loads the size of the array to CX
MOV DX, MAXV ;loads the maxv variable to DX
THELOOP: ;This is where the loop starts
MOV BX,INTARR[SI] ;loads the array item at index SI
CMP MAXV,BX ;compares BX to the value of MAXV
JGE SKIP ;goes to SKIP if MAXV is greater
MOV MAXV,BX ;if BX is greater BX moves to DX
SKIP: ;this is the skip label
INC SI ;increments SI for the next loop
LOOP THELOOP ;loops
CODE ENDS ;ends the segment
END START ;ends start label
ret
我正在使用 emu8086。当我模拟它并观察变量时,它指出 MAXV 是 0900h。它通过大约 20 个 NOP 命令运行。任何帮助将不胜感激!
好的,
这是我在您的代码中看到的所有问题:
- SIZEA和MAXV是地址。因此,当您编写
MOV CX,SIZEA
时,您不会加载SIZEA的内容,而是加载其地址。这不是你想要的。你想写MOV CX,[SIZEA]
- 我不确定 emu8086 是如何工作的,因为我从未使用过它。不过,您设置数据细分受众群的方式看起来有点可疑。您不是将偏移地址加载到段寄存器中吗?
ret
是函数的一部分,应位于代码段中。但是我不知道是否有任何有效的代码可以返回。- 第一个 lea 指令没有做你想要它做的事情,只是删除它
- 第一个
MOV BX,INTARR[SI]
是多余的,可以删除。 - 您只将 SI 增加一个字节,但数组条目的长度是一个字(2 个字节)
这是我为使您的代码正常工作而更改的内容。请注意,这不是针对 emu8086 而是标准 NASM:
START: XOR AX,AX ; Clear data segment, may not be how you do it on emu8086
MOV DS,AX
MOV SI,AX ; Clear the Source index
MOV CX,[SIZEA]
XOR DX,DX ; Note how I just clear DX, no need to load 0 from memory
; DX will then contain highest value
THELOOP:MOV BX,[INTARR+SI] ; I am just using this syntax because it is the standard for NASM
CMP DX,BX ; Compare max value with current value
JGE SKIP
MOV DX,BX ; Now just move the max value to the register
SKIP: ADD SI,2 ; Move by 2 bytes since it is a word array
LOOP THELOOP ;
MOV [MAXV],DX ; Now you can save the highest value to memory
; less memory access so faster execution
RET
; DATA
SIZEA DW 10
MAXV DW 0
INTARR DW 0x1,0x2,0x4,0xFF,0xFE,0xFA,0x7,0x9,0x8,0xFD