英文:
ASSEMBLY 8086 EXERCISE UNIVERSITY
问题
有人能否为Intel 8086处理器的汇编程序开发一个过程,该过程从数据段中读取一个整数向量,并用数据段中定义的VAL值替换向量的前DIM/2个元素?
我尝试编写了以下内容:
STACKSEGMENT SEGMENT PUBLIC 'DATACLASS'
DB 8 DUP(0)
STACKSEGMENT ENDS
DATASEGMENT SEGMENT PUBLIC 'DATACLASS'
VECTOR DW 4 DUP (?)
DIM DW 4
VAL DW 5
DATASEGMENT ENDS
MYCODESEG SEGMENT PARA PUBLIC 'CODECLASS'
MYPROC PROC NEAR
ASSUME CS: MYCODESEG, DS:DATASEGMENT, SS:STACKSEGMENT
MOV AX, DATASEGMENT
MOV DS, AX
LEA AX, VECTOR
PUSH AX
MOV AX, DIM
PUSH AX
MOV AX, VAL
PUSH AX
CALL MYPROC
ADD SP,10
mov ah, 4Ch
int 21h
MYCODESEG ENDS
END MYPROC
END
英文:
is anyone able to develop a procedure in an assembly program for intel 8086 processor that reads a vector of integers from data segment and replaces the first DIM/2 elements of the vector with the defined VAL value of the data segment ?
I tried to write something:
STACKSEGMENT SEGMENT PUBLIC 'DATACLASS'
DB 8 DUP(O)
STACKSEGMENT ENDS
DATASEGMENT SEGMENT PUBLIC 'DATACLASS'
VECTOR DW 4 DUP (?)
DIM DW 4
VAL DW 5
DATASEGMENT ENDS
MYCODESEG SEGMENT PARA PUBLIC 'CODECLASS'
MYPROC PROC NEAR
ASSUME CS: MYCODESEG, DS:DATASEGMENT, SS:STACKSEGMENT
MOV AX, DATASEGMENT
MOV DS, AX
LEA AX, VECTOR
PUSH AX
MOV AX, DIM
PUSH AX
MOV AX, VAL
PUSH AX
CALL MYPROC
ADD SP,10
mov ah, 4Ch
int 21h
MYCODESEG ENDS
END MYPROC
END
答案1
得分: 1
如果你尝试将`VECTOR`的内容读入`AX`,这样做是不行的。
MOV AX, DATASEGMENT
MOV DS, AX
这是正确的。
---
LEA AX, VECTOR
PUSH AX
然而,这样做不会让你得到`VECTOR`的内容。`LEA`代表"加载有效地址"。记住,标签`VECTOR`只是数据存储的位置,而不是数据本身。实质上,你只是做了
mov ax, offset VECTOR
push ax
这并不能让你更接近你想要的。在这个CPU上,你实际上不能使用`ax`从内存中读取数据。只能使用`bx`、`bp`、`si`和`di`。
这才是你真正想要的:
MOV AX, DATASEGMENT
MOV DS, AX
LEA SI, VECTOR
MOV AX, [SI]
在`SI`周围加上括号就是内存加载操作。这将把向量数组中的第一个16位值加载到`AX`中。如果没有括号,即`MOV AX, SI`,你只是把向量数组的地址移到AX中,这不会实现任何目标。
请注意,你将`VECTOR`声明为`VECTOR DW 4 DUP (?)`,这告诉汇编器"我在这里需要4个字的空间,但我现在不关心值是什么,因为我即将编写的代码会填充它们。"因此,从一开始就读取这些数据不会得到任何有价值的东西。使用上面的代码示例,`ax`中会得到*一些*数据,但不能保证你得到的数据有任何真正的意义。
英文:
MOV AX, DATASEGMENT
MOV DS, AX
LEA AX, VECTOR
PUSH AX
MOV AX, DIM
PUSH AX
MOV AX, VAL
PUSH AX
CALL MYPROC
ADD SP,10
If you're trying to read the contents of VECTOR
into AX
, this isn't going to do it.
MOV AX, DATASEGMENT
MOV DS, AX
This is correct.
LEA AX, VECTOR
PUSH AX
This, however, won't get you to the contents of VECTOR
. LEA
stands for "load effective address". Remember that the label VECTOR
is just where the data is stored - not the data itself. Essentially you've just done
mov ax, offset VECTOR
push ax
which doesn't really get you any closer to what you want. On this CPU, you actually can't use ax
to read from memory like that. Only bx
, bp
, si
, and di
can.
So here's what you're really looking for:
MOV AX, DATASEGMENT
MOV DS, AX
LEA SI, VECTOR
MOV AX, [SI]
Putting the brackets around SI
is what makes it a memory load operation. This takes the first 16-bit value in your vector array and loads it into AX
. Without the brackets there, i.e. MOV AX, SI
, you'd just be moving the address of your vector array into AX which doesn't achieve anything.
Now that being said, you declared VECTOR
as VECTOR DW 4 DUP (?)
which tells the assembler "I need 4 words of space here, but I don't care what the values are right now because the code I'm going to write is going to fill them in." So reading that data right from the start isn't going to accomplish anything of value. You'll get something into ax
with the code example above, but there's no guarantee that the data you get has any real meaning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论