【问题标题】:x86 interrupt based assembly variable access problem基于 x86 中断的程序集变量访问问题
【发布时间】:2011-05-08 14:57:38
【问题描述】:

我有一个(看似)简单的问题,要读取一个字符串并使用基于 x86 中断的程序集再次打印出来。我遇到的问题是访问已正确读取的字符串。变量 - input db 20, 0, " " 是我的初始字符串。在我调用输入中断之后,0 现在应该保存字符串的长度,当我调用打印中断时,我需要存储并传递给cx。 20 是输入的最大长度。我最终遇到了两个问题 - 如何访问字符串的长度(我使用的是任意数字,要么将其缩短或在结束后打印垃圾),以及如何在没有数字位的情况下访问字符串开始?任何帮助表示赞赏,我的尝试是:

(我在win 7 32位下使用tasm & Tlink,也在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

【问题讨论】:

    标签: assembly x86 interrupt


    【解决方案1】:

    您在此处拥有的内容称为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
    

    【讨论】:

      【解决方案2】:
      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

      【讨论】:

        猜你喜欢
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多