Reverse Program emu8086

1k views Asked by At

I need to write a program that reverse the letters order. I wrote the following program but it doesn't work for me.

Please help me fix my program:

org  100h
jmp main 
string      db  'm', 'g', 'g', 'i', 'h', 's', 'f', 'g', 'm',0Dh,0Ah,'$'
main:  
lea ax,string
push 9
push offset string
call reverse  
lea ax,string
mov ah,0
int 16h
ret
reverse proc
push bp
mov bp,sp
sub sp,6;
len equ [bp + 6]  
i equ [bp - 4]       
temp equ [bp - 2]   
mov ax,[bp+6]
mov len,ax
dec len
mov dx,len
mov i,ax 
mov bx,[bp+4] 
mov ax,i
for1:               
mov si,i            
mov cl, [bx+si]
mov dl, cl      
mov di, ax
sub di, si
mov [bx+di],cl
mov cl,dl
mov [bx+si], cl 
mov ax,len
mov cl,2
div cl 
dec i
cmp i,ax
jge for1  
mov sp,bp
pop bp
ret
reverse endp</code>

........................................ Uri.

1

There are 1 answers

3
Jose Manuel Abarca Rodríguez On BEST ANSWER

You had some unnecessary code, and some wrong code. I made some changes to make it run, here it is (the comments explain the changes), copy-paste it on EMU8086 and run it :

org  100h
jmp main 
string      db  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',0Dh,0Ah,'$'
i           dw ?   ;POINTER TO STRING FROM LEFT TO RIGHT.
j           dw ?   ;POINTER TO STRING FROM RIGHT TO LEFT.
len         dw ?   ;LEN IS BETTER HERE FOR DEBUGGING.
main:  
lea ax,string
push 9
push offset string
call reverse  
lea ax,string
mov ah,0
int 16h
ret
reverse proc
push bp
mov bp,sp
;sub sp,6;                     ;UNNECESSARY.
;len equ [bp + 6]              ;UNNECESSARY.
;i equ [bp - 4]                ;UNNECESSARY.
;temp equ [bp - 2]             ;UNNECESSARY.
mov ax,[bp+6]
mov len,ax
;dec len                       ;UNNECESSARY.
;mov dx,len                    ;UNNECESSARY.
;mov i,ax                      ;UNNECESSARY.
;mov bx,[bp+4]                 ;UNNECESSARY.
;mov ax,i                      ;UNNECESSARY.
mov i, offset string           ;I POINTS TO FIRST ELEMENT.
mov j, offset string           ;J POINTS TO FIRST ELEMENT.
add j, ax                      ;J POINTS BEYOND LAST ELEMENT.
dec j                          ;J POINTS TO LAST ELEMENT.
shr len, 1                     ;LEN / 2 (50% OF STRING WILL BE SWAPPED).
for1:               
mov si,i                       ;SI POINTS TO FIRST ELEMENT.
mov di,j                       ;DI POINTS TO LAST ELEMENT.
mov cl,[si]                    ;CL = FIRST ELEMENT.
mov dl,[di]                    ;DL = LAST ELEMENT.      
;mov di, ax
;sub di, si                    ;UNNECESSARY.
mov [si],dl                    ;LAST ELEMENT REPLACES FIRST.
;mov cl,dl                     ;UNNECESSARY.
mov [di], cl                   ;FIRST ELEMENT REPLACES LAST. 
;mov ax,len                    ;UNNECESSARY.
;mov cl,2                      ;UNNECESSARY.
;div cl                        ;UNNECESSARY.
;dec i                         ;UNNECESSARY.
;cmp i,ax                      ;UNNECESSARY.
inc i                          ;NEXT ELEMENT.
dec j                          ;PREVIOUS ELEMENT.
dec len                        ;(LEN/2)--.
cmp len, 0                     ;IF ( LEN >= 0 ) ...
jge for1  
;mov sp,bp                     ;UNNECESSARY.
pop bp
ret
reverse endp