x86基于中断的汇编变量访问问题

发布于 2024-10-02 02:56:50 字数 1624 浏览 4 评论 0原文

我有一个(看似)简单的问题,要读取字符串并使用基于 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 技术交流群。

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

发布评论

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

评论(2

舂唻埖巳落 2024-10-09 02:56:50

这里的内容被称为Pascal String。原始版本(用于 16 位 Pascal 语言)使用第一个字节来保存字符串的长度,其余字节包含实际字符串(不是以零结尾)。这给出了 255 字节的最大长度。

该版本使用 32 位 Delphi,使用略有不同的方法:

struct {
  DWORD allocated_size;
  DWORD used_size;
  char* buff;
};

它与您的情况类似,但您使用 BYTE 来表示大小而不是 DWORD。
使用它们的标准方法是保留指向实际字符串的指针,并对特殊字段使用负偏移量,例如:

lea ax, [input + 2]  //; standard string, could need a trailing '\0'
mov al, BYTE PTR [input+2 - 1] //; strlen
mov al, BYTE PTR [input+2 - 2] //; allocated buff size

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:

struct {
  DWORD allocated_size;
  DWORD used_size;
  char* buff;
};

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:

lea ax, [input + 2]  //; standard string, could need a trailing '\0'
mov al, BYTE PTR [input+2 - 1] //; strlen
mov al, BYTE PTR [input+2 - 2] //; allocated buff size
塔塔猫 2024-10-09 02:56:50
inputBuffer LABEL BYTE 
maxChar     BYTE 10 
numinput    BYTE ? 
buffer      BYTE 10 DUP (0) 
... 
     mov  ah, 0Ah  ; string input 
     mov  dx, OFFSET inputBuffer 
     int  21h

将您的 inputBuffer 放入 .data 部分,它作为中断 21 使用的结构(用读取的字符数填充 numinput0

inputBuffer LABEL BYTE 
maxChar     BYTE 10 
numinput    BYTE ? 
buffer      BYTE 10 DUP (0) 
... 
     mov  ah, 0Ah  ; string input 
     mov  dx, OFFSET inputBuffer 
     int  21h

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文