x86基于中断的汇编变量访问问题
我有一个(看似)简单的问题,要读取字符串并使用基于 x86 中断的程序集再次打印出来。我遇到的问题是访问已正确读取的字符串。变量 - input db 20, 0, " "
是我的初始字符串。调用输入中断后,0 现在应该保存字符串的长度,当我调用打印中断时,我需要存储该字符串并将其传递给 cx
。 20 是输入的最大长度。我最终遇到了两个问题 - 如何访问字符串的长度(我使用了任意数字,它要么将其截短,要么在结尾后打印垃圾)以及如何访问字符串中没有数字位的字符串开始?感谢任何帮助,我的尝试是:(
我在 win 7 32 位下使用 tasm 和 Tlink,也在 dos 框模拟下使用)
;7. Read in a String of characters and Print the string back out.
.model small
.stack 100h
.data
colour db 00001111b
input db 20, 0, " "
strlen dw 20; this should be ?
.code
main:
call initsegs
call readstring
call printstring
call exit
PROC printstring
push ax bx cx dx bp
mov ah, 13h ; int 13h of 10h, print a string
mov al, 1 ; write mode: colour on bl
mov bh, 0 ; video page zero
mov bl, colour; colour attribute
mov cx, strlen; getting this is the problem
mov dh, 10; row
mov dl, 10; column
mov bp, offset input ; es:bp needs to point at string..this points to string but includes its max and length at the start
int 10h;
pop bp dx cx bx ax
ret
ENDP printstring
PROC readstring
push ax dx
mov ah, 0ah ; function a of 21h - read a string
mov dx, offset input ; reads string into DS:DX so DX needs be offset of string variable
int 21h ; call the interrupt
;mov strlen ....something
pop dx ax
ret
ENDP readstring
PROC exit
mov ah, 4ch
INT 21h
RET
ENDP Exit
PROC initsegs
push ax
mov ax, @DATA
mov ds, ax
mov es, ax
pop ax
RET
ENDP initsegs
end main
I have a (seemingly) simple question to read in a string and print it out again using x86 interrupt based assembly. The problem i'm having is accessing the string that has been read in properly. The variable - input db 20, 0, " "
is my initial string. After I call the input interrupt, the 0 should now hold the length of the string, which I need to store and pass to cx
when I call the print interrupt. 20 is the max length of input. I end up with two problems - how do I access the length of the string (I've using an arbitrary number, which either chops it short or prints garbage after the end) and how do I access the string without the number bit at the start? Any help appreciated, my attempt is:
(I use tasm & Tlink under win 7 32 bit, and also under dos box emulation)
;7. Read in a String of characters and Print the string back out.
.model small
.stack 100h
.data
colour db 00001111b
input db 20, 0, " "
strlen dw 20; this should be ?
.code
main:
call initsegs
call readstring
call printstring
call exit
PROC printstring
push ax bx cx dx bp
mov ah, 13h ; int 13h of 10h, print a string
mov al, 1 ; write mode: colour on bl
mov bh, 0 ; video page zero
mov bl, colour; colour attribute
mov cx, strlen; getting this is the problem
mov dh, 10; row
mov dl, 10; column
mov bp, offset input ; es:bp needs to point at string..this points to string but includes its max and length at the start
int 10h;
pop bp dx cx bx ax
ret
ENDP printstring
PROC readstring
push ax dx
mov ah, 0ah ; function a of 21h - read a string
mov dx, offset input ; reads string into DS:DX so DX needs be offset of string variable
int 21h ; call the interrupt
;mov strlen ....something
pop dx ax
ret
ENDP readstring
PROC exit
mov ah, 4ch
INT 21h
RET
ENDP Exit
PROC initsegs
push ax
mov ax, @DATA
mov ds, ax
mov es, ax
pop ax
RET
ENDP initsegs
end main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的内容被称为
Pascal String
。原始版本(用于 16 位 Pascal 语言)使用第一个字节来保存字符串的长度,其余字节包含实际字符串(不是以零结尾)。这给出了 255 字节的最大长度。该版本使用 32 位 Delphi,使用略有不同的方法:
它与您的情况类似,但您使用 BYTE 来表示大小而不是 DWORD。
使用它们的标准方法是保留指向实际字符串的指针,并对特殊字段使用负偏移量,例如:
What you have here is known as
Pascal String
. The original version (used in 16bit Pascal language) used first byte to hold the length of the string, and the rest of bytes contaned the actual string (not zero terminated). This gives maximum length of 255 bytes.The version uses 32bit Delphi used slighly different approach:
It's similar to your case, but you use BYTE for size instead of DWORD.
The standard way to work with them is to keep the pointer to the actual character string, and to use negative offsets for special fields, like in:
将您的 inputBuffer 放入 .data 部分,它作为中断 21 使用的结构(用读取的字符数填充 numinput0
Put your inputBuffer into the .data section, it works as a struct that is used by interrupt 21 (filling numinput with the number of characters read0