Unable to reverse string in 8086 MASM

2019-07-30 14:50发布

I've written some 8086 assembly code to reverse a string. I'm relatively new to assembly so please bear with me.

The logic is that I define a string called 'str1'. I move this into the SI Register. Suppose string 'str1' is "Hello$", then I load the address of 'str1'+5 into SI. Now, I load an address, say 5000 into DI. And I load each character from SI into DI and everytime I increment SI and decrement SI till 5 times.

Here is the code

assume cs:code,ds:data
data segment
str db "Hello$"
data ends
code segment
start:
mov ax,data
mov ds,ax
cld
mov cx,5h
mov bx,5h
lea si,str
add si,5
mov di,5000h
l1:mov bx,[si]
mov [di],bx
dec si
inc di
loop l1
hlt
code ends
end

I get an absolute garbage value when I access location 5000. Plz help thanks

1条回答
迷人小祖宗
2楼-- · 2019-07-30 15:23

Your code is almost good, you only need an auxiliary string (if such thing is allowed) :

assume cs:code,ds:data
data segment
str db "Hello$"
aux db "     $"       ;AUXILIARY STRING.
data ends
code segment
start:
mov ax,data
mov ds,ax
cld
mov cx,5h
mov bx,5h
lea si,str
add si,4              ;0..4 = 5.
lea di,aux            ;POINT TO AUXILIARY.
l1:mov bl,[si]        ;YEAH, LET'S USE
mov [di],bl           ;"BL" INSTEAD OF "BX".
dec si
inc di
loop l1
hlt
code ends
end
查看更多
登录 后发表回答