基于x86中断的程序集变量访问问题

时间:2010-11-11 17:29:21

标签: assembly x86 interrupt

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

(我在win 7 32位下使用tasm& Tlink,也在dos box模拟下使用)

;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

2 个答案:

答案 0 :(得分:0)

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使用的结构(填充numinput,字符数为read0

答案 1 :(得分:0)

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

该版本采用32bit 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