将字符串存储在变量中并用 8086 汇编语言打印出来
Storing a String in Variable and printing it out in 8086 Assembly language
;CODE FOR PRINTING A STRING IN 8086 ASSEMBLY LANGUAGE:
.model small
.stack 100h
.data
msg db 'hello$'
.code
main proc
mov dx,@data
mov ds,dx
mov dx,offset msg ;lea dx,msg
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
我的问题:
- db 可以存储 8 位数据,但 hello$ 的大小为 6 字节(1 个字符 = 8 位)。如果字符串大于数据库的容量,它如何存储字符串?
- 如果我写 MOV DX,MSG 它会显示错误(因为 dx 是 8 位寄存器并且字符串比它的容量大)。但当它被写为 MOV DX,OFFSET msg 或 LEA DX,msg 时它起作用。你能解释一下 offset 和 lea 的作用吗?
offset 存储第一个字符的地址。在这种情况下,它是 'h'..所以 dx 不包含整个字符串,而是第一个字符的地址,可能 lea 也可以作为偏移量...如果我是对的,它解决了第二个问题..但是如何来数据库存储 6 字节字符?因为它只能处理 8 位或 1 个字节
db can store 8 bits of data but hello$ is 6 byte in size(1 char= 8 bits). how can it store the string if the string is larger then db's capacity?
它不存储 hello$
作为一个整体,而是存储 hello$
的 OFFSET,在本例中为 h(字符串的起始字符 ).
这是您的字符串在内存中的存储方式:
假设 DS:SI 寄存器(指向一些内存 address/location 通常是您在程序中声明的变量的地址。)存储地址 07200
。并假设已为该地址分配了偏移量(在您的情况下为 'h' )。现在偏移量后面的所有其他字符将存储在连续的内存位置,因此地址 07200
将存储 h
,07201
将存储 e
,07202
将存储l
等等。所以这样 msg
变量将只需要存储偏移量,因为它知道它会找到偏移量之后的字符串的所有其他字符(数组存储在连续的内存中)。
if i write MOV DX,MSG it shows error(as dx is 8 bit register and the string is larger then it's capacity). but it works when it is written as MOV DX,OFFSET msg or LEA DX,msg. can you explain what offset & lea does?
首先 DX 不是 8 位而是它的 16 位寄存器和 09h service of INT 21h
needs an offset of your string to be placed in DX
register and then from there it keeps printing the characters on the console until it encounters $ ( string termination character ) so writing msg DX, MSG
upsets 09h
service of INT 21h
that is why it throws an error. mov dx, offset msg
and lea dx, msg
( Load Effect Address ) 都将字符串的偏移量放在 DX 寄存器中。
;CODE FOR PRINTING A STRING IN 8086 ASSEMBLY LANGUAGE:
.model small
.stack 100h
.data
msg db 'hello$'
.code
main proc
mov dx,@data
mov ds,dx
mov dx,offset msg ;lea dx,msg
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
我的问题:
- db 可以存储 8 位数据,但 hello$ 的大小为 6 字节(1 个字符 = 8 位)。如果字符串大于数据库的容量,它如何存储字符串?
- 如果我写 MOV DX,MSG 它会显示错误(因为 dx 是 8 位寄存器并且字符串比它的容量大)。但当它被写为 MOV DX,OFFSET msg 或 LEA DX,msg 时它起作用。你能解释一下 offset 和 lea 的作用吗?
offset 存储第一个字符的地址。在这种情况下,它是 'h'..所以 dx 不包含整个字符串,而是第一个字符的地址,可能 lea 也可以作为偏移量...如果我是对的,它解决了第二个问题..但是如何来数据库存储 6 字节字符?因为它只能处理 8 位或 1 个字节
db can store 8 bits of data but hello$ is 6 byte in size(1 char= 8 bits). how can it store the string if the string is larger then db's capacity?
它不存储 hello$
作为一个整体,而是存储 hello$
的 OFFSET,在本例中为 h(字符串的起始字符 ).
这是您的字符串在内存中的存储方式:
假设 DS:SI 寄存器(指向一些内存 address/location 通常是您在程序中声明的变量的地址。)存储地址 07200
。并假设已为该地址分配了偏移量(在您的情况下为 'h' )。现在偏移量后面的所有其他字符将存储在连续的内存位置,因此地址 07200
将存储 h
,07201
将存储 e
,07202
将存储l
等等。所以这样 msg
变量将只需要存储偏移量,因为它知道它会找到偏移量之后的字符串的所有其他字符(数组存储在连续的内存中)。
if i write MOV DX,MSG it shows error(as dx is 8 bit register and the string is larger then it's capacity). but it works when it is written as MOV DX,OFFSET msg or LEA DX,msg. can you explain what offset & lea does?
首先 DX 不是 8 位而是它的 16 位寄存器和 09h service of INT 21h
needs an offset of your string to be placed in DX
register and then from there it keeps printing the characters on the console until it encounters $ ( string termination character ) so writing msg DX, MSG
upsets 09h
service of INT 21h
that is why it throws an error. mov dx, offset msg
and lea dx, msg
( Load Effect Address ) 都将字符串的偏移量放在 DX 寄存器中。