Trouble when reverse and display string in assembly

时间秒杀一切 提交于 2019-12-12 02:44:00

问题


here is my code:

model small
stack 100h

.data
string1 db "Khoa$"
string2 db 4 dup(?)
count dw 4
.code
begin:  
mov ax,@data
mov ds,ax
mov es,ax
mov cx,count
mov si,0
mov di,0
add di,count
dec di

again:  mov al,string1[si]
mov string2[di],al
inc si
dec di
loop again

mov ah,9
lea dx,string1
int 21h

end begin

in this part

mov ah,9
lea dx,string1
int 21h

when i try to display string1 to console, it's ok. but no result with string2.

i'm new to assembly. Please help!


回答1:


Although this is homework, this is a reasonable attempt at a solution. I don't know what assembler you are using - it looks like MASM but I'm unsure how these 2 lines could assemble without a syntax error:

model small
stack 100h

I think this should be (note the periods in front as they are assembler directives):

.model small
.stack 100h

string1 is initially 5 characters long including the $ to terminate the string. When using int 21h/ah 9 strings need to be terminated with $ or it will continue to print junk beyond the end of your string. With that in mind your output buffer should be long enough to hold 5 characters including the $ so string2 should be defined as:

string2 db 5 dup(?)

The primary problem in your code is terminating the string with a $. One way would be to place the $ in the last position of the array when you set up the si and di registers initially:

mov si,0
mov di,0
add di,count
mov string2[di],'$'   ; Put string terminator in last position
dec di

Also note:

mov di,0
add di,count

Could be written simply as:

mov di, count

If you are using MASM, to gracefully exit your 16 bit program you might want to put a .exit directive just before your end statement:

.exit
end begin


来源:https://stackoverflow.com/questions/32568014/trouble-when-reverse-and-display-string-in-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!