错误而在8086倒车串 [英] Error while reversing string in 8086
问题描述
我有这样的code逆转使用8086 ALP的字符串。但随着预期和进入一个无限循环或打印一些随机的性格特征的code不起作用。
.MODEL小
.STACK 100H
。数据
DB的String1汇编语言程序设计$
长DW $ -String1-1。code
主要PROC
MOV AX,@data
MOV DS,AXMOV SI,偏移的String1
MOV CX,长度
ADD SI,CX返回:MOV DL,[SI]
MOV AH,02H
INT 21H
DEC SI
环回MOV AH,4CH
INT 21H
主要ENDP
最终主
您code有2个问题:
-
$
表示偏移当前编译指令,它可以用在一些偏移算法,但方式,你可以声明你长度
数据不是做你的想法。其实,长度
是包含偏移本身,减去失调字符串1
中,减1使用的常用方法$
来计算的长度是使用EQU
恒权的字符串声明之后,如:DB的String1汇编语言程序设计$
长EQU $ -String1也就是说,
MOV CX,长度
将加载CX
与字符串的长度,不是内存抵消。一个EQU
不参与所产生的程序的任何地方。如果反正你想在内存中的地方与字符串的长度,而不是仅在汇编时定义它,你可以做DB的String1汇编语言程序设计$
STRL EQU $ -String1
长DW STRL;初始化数据将是字符串的长度,不属于抵销 -
您code开始扭转串1个字节过长,作为一个1字节的字符串,你不必添加任何东西,所以例如,一个
DEC SI
将纠正偏移。
以下(略)修改的程序做你想要什么:
.MODEL小
.STACK 100H
。数据
DB的String1汇编语言程序设计$
长EQU $ -String1-1。code
主要PROC
MOV AX,@data
MOV DS,AXMOV SI,偏移的String1
MOV CX,长度
ADD SI,CX
DEC SI返回:MOV DL,[SI]
MOV AH,02H
INT 21H
DEC SI
环回MOV AH,4CH
INT 21H
主要ENDP
最终主
I have this code to reverse a string using 8086 ALP. But the code does not work as intended and gets into a infinite loop or prints some random charecter.
.model small
.stack 100h
.data
String1 DB "Assembly Language Program$"
Length dw $-String1-1
.code
Main proc
MOV AX, @data
MOV DS, AX
MOV SI, Offset String1
MOV CX, Length
ADD SI, CX
Back: MOV DL, [SI]
MOV AH, 02H
INT 21H
DEC SI
LOOP Back
MOV AH, 4CH
INT 21H
Main endp
End main
You code has 2 issues:
$
stands for "the offset of the current compiling instruction", and it can be used in some offset arithmetic, but the way you are declaring yourlength
data is not doing what you think. Actually,length
is containing the offset of itself, minus the offset ofString1
, minus 1. The common way to use$
to compute a length is by using anequ
constant right after the string declaration, like:String1 DB "Assembly Language Program$" Length EQU $-String1
That is, the
MOV CX, Length
will loadCX
with the length of the string and not a memory offset. AnEQU
does not take any place in the resulting program. If anyway you would like to have a place in memory with the string length and not only defining it at assembly time, you can doString1 DB "Assembly Language Program$" strl EQU $-String1 Length DW strl ;the initialized data will be the string length, not an offset
your code start to reverse the string 1 byte too long, as for a 1 byte string you don't have to add anything, so e.g a
DEC SI
will correct the offset.
The following (slightly) modified program do what you want:
.model small
.stack 100h
.data
String1 DB "Assembly Language Program$"
Length equ $-String1-1
.code
Main proc
MOV AX, @data
MOV DS, AX
MOV SI, Offset String1
MOV CX, Length
ADD SI, CX
DEC SI
Back: MOV DL, [SI]
MOV AH, 02H
INT 21H
DEC SI
LOOP Back
MOV AH, 4CH
INT 21H
Main endp
End main
这篇关于错误而在8086倒车串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!