首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DOSBox调试程序集

DOSBox调试程序集
EN

Stack Overflow用户
提问于 2016-09-09 01:33:32
回答 1查看 3K关注 0票数 0

我正在使用调试实用工具运行DOSBox来构建程序集代码。我只是想弄清楚怎么用字符串来读。到目前为止我有这个。

代码语言:javascript
复制
-n test.com
-a
072A:0100 db 15
072A:0101 db 16
072A:0102 mov dx, 100
072A:0105 mov ah, 0A
072A:0107 int 21
072A:0109 int 20

-rcx 15
-w
-q

因此,我可以从缓冲区读取15个或更少字符的字符串。当我读到字符串时,它会放在哪里?我想试着把绳子打印出来。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-09 06:29:36

示例中的代码有两个问题。

您不希望将原始数据放置在偏移量100,因为DOS将从这个地址开始执行。因此,从偏移量102开始的代码将无法正确执行,因为它将组装到“AX,BA16”指令,该指令从"mov,100“指令中窃取BA字节,其余指令将被误解。

如果您没有问题#1,第二个问题是输入字符将覆盖从偏移量102开始的代码(因为100处缓冲区的前两个字节是保留的)。如果您可以让代码执行,这可能不是您想要的。;)

您不希望在偏移量100处定义任何数据,因为这是.COM程序的入口点。处理此问题的一个简单方法是在代码之后定义数据。在使用调试之类的方法时,您必须直接指定内存位置,而无需使用标签,必须考虑组装操作数的大小。

一种方法是从地址100开始组装代码,并为字符串和缓冲区地址使用占位符;然后在定义数据后修复这些指令。

这是您修改过的样本:

注意:我忍不住在字符串输入函数之前添加了一个输出提示符。

代码语言:javascript
复制
-n test2.com
-a
0100: mov dx,100    ;100 is a placeholder for the address of output prompt
0103: mov ah,9
0105: int 21        ;output prompt
0107: mov dx,100    ;100 is a placeholder for the address of the input buffer
010A: mov ah,0a
010C: int 21        ;read user input
010E: int 20
0110: db 48         ;"H"
0111: db 65         ;"e"
0112: db 6c         ;"l"
0113: db 6c         ;"l"
0114: db 6f         ;"o"
0115: db 3a         ;":"
0116: db 20         ;<space>
0117: db 24         ;"$" / DOS strings terminated by dollar sign
0118: db 0f         ;buffer member: hold 15 chars
0119: db 00         ;buffer member: character input count stored here

;the remainder of the .COM memory segment can be used to store the data
; starting at address 11A

;now that we know where our data fits, lets plug the addresses in

-a 100
0100: mov dx,110    ;address of "H" in prompt string
-a 107
0100: mov dx,118    ;address of input buffer with first two bytes reserved (see above)
                    ;max length to read and characters read

-rcx
CX 0000
:1a
-w
Writing 001A bytes

;lets run the program

-g
Hello: <lets assume you type "cat" followed by RETURN>
Program terminated normally

;now look at the 2nd byte in the buffer for the number of characters typed
;   along with the character data entered

-d 118 11e
0110          0F 03 63 61 74 0D 0D      ..cat..

我认为一旦定义了最大输入字符成员,就不必担心为缓冲区预留空间;因为缓冲区位于程序的末尾,所以您可以访问64k段的其余部分作为缓冲区空间。(64k减去代码和环境空间的0x11b字节)。

编辑2016/09/11

下面是一个经过修改的版本(附有解释),以解决您有关输出用户输入内容的问题:

代码语言:javascript
复制
0100  MOV     DX,0140
0103  MOV     AH,09
0105  INT     21            ;output prompt
0107  MOV     DX,014B
010A  MOV     AH,0A
010C  INT     21            ;read user input
010E  MOV     DX,0148
0111  MOV     AH,09
0113  INT     21            ;output CRLF
0115  MOV     DI,[014C]     ;load DI with value of characters read
0119  AND     DI,00FF       ;   MOV above read WORD, but we only want lower byte
011D  LEA     DI,[DI+014D]  ;point DI to end of input string
0121  MOV     AL,24         ;load DOS terminator char "$" in AL
0123  STOSB                 ;write it to end input string
0124  MOV     DX,014D
0127  MOV     AH,09
0129  INT     21            ;print the input string
012B  INT     20            ;exit program

;I inserted NOPS until address 0140 so I would have room to insert code without my
;   data offsets changing

0140 DB 48                  ;"Hello: $" prompt begin
0141 DB 65
0142 DB 6C
0143 DB 6C
0144 DB 6F
0145 DB 3A
0146 DB 20
0147 DB 24
0148 DB 0D                  ;CRLF$ sequence begin
0149 DB 0A
014A DB 24
014B DB 20                  ;Buffer begin (32 bytes max length)
014C DB 00

实现相同结果的方法有多种,例如用用户输入计数加载CX寄存器,以及使用循环指令使用适当的DOS函数打印每个字符。祝好运!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39402521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档