News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Using rep movsb

Started by Quang, April 02, 2006, 10:43:34 AM

Previous topic - Next topic

Quang

Sorry for giving all the code, but the circumstances make me do so.


.model small

.stack 100h

;------------------------------------------------------------------------------------------

.data


   
messt   db 'The string itself: $'     
         
mesreal db 'The real length of string: $'     

mesw    db 'Enter the string: $'     

mesp    db 'Enter the position frome where to copy: $'   

mesl    db 'Enter the length of the string to copy: $'     

mesend  db 'The copied string: $'

pos     db ?                                                ; position from where to copy

len     db ?                                                ; length of substring
                                                           
maxlen  equ 20                                            ; max length of string

actlen  equ ?                                               ; real length of string

buffer  db maxlen , actlen , maxlen dup (' ')               ; string       

string  db maxlen dup ('0')                               ; where to copy

cur     db ?                                                ; cursor pos       
                                                         
;---------------------------------------------------------------------------------------------



.code 

start:         

    mov ax,@data
    mov ds,ax   
   


;--------INPUT INVITATION----------------------------------   

    mov ah, 03h
    int 10h     
    mov cur, dh
   
    add cur, 1             
    mov dh, cur                       ; cursor
    mov ah, 2
    int 10h                     
   
    lea dx, mesw                      ; invitation to input
    mov ah, 9
    int 21h

;----------------------------------------------------------     


             
                                       
;-----------STRING INPUT-----------------------------------
   
    lea dx, buffer
    mov ah, 0ah
    int 21h 
;----------------------------------------------------------

   
;-------ACTUAL LENGTH OUTPUT----------------------------------     
   
    add cur, 1
    mov dh, cur                       ; cursor
    mov dl, 0
    mov ah,2
    int 10h
       
    mov ah, 9
    lea dx, mesreal 
    int 21h
         
    mov ax, word ptr buffer[1] 
    mov ah,0
   
    call printw
   
   
   
;------------------------------------------------------------     
;-----------STRING OUTPUT----------------------------------
   
    add cur, 1
    mov dh, cur                       ; cursor
    mov dl, 0
    mov ah,2
    int 10h
 
    mov dx, offset messt
    mov ah, 9
    int 21h 
   
    xor bx,bx
        mov bl, buffer[1]
        mov buffer[bx+2], '$'
        mov dx, offset buffer + 2
        mov ah, 9
        int 21h       
;-----------------------------------------------------------   
;--------GETTING THE POS & LENGTH----------------------------
   
; position ----   
    add cur, 1
    mov dh, cur                     ; cursor
    mov dl, 0
    mov ah, 2
    int 10h
   
    mov ah, 9
    lea dx, mesp
    int 21h       
   
    mov ah, 1   
    int 21h
    mov pos, al
    sub pos, 30h                    ; converting to number
 
   
; length ------

    add cur, 1
    mov dh, cur                     ; cursor
    mov dl, 0
    mov ah, 2
    int 10h
   
    mov ah, 9
    lea dx, mesl
    int 21h     
   
    mov ah, 1   
    int 21h
    mov len, al     
    sub len, 30h                    ; converting to number
 
   
;----------COPYING--------------------------------------------   

    add cur, 1
    mov dh, cur                     ; cursor
    mov dl, 0
    mov ah, 2
    int 10h 
   
    mov ah, 9
    lea dx, mesend
    int 21h   
   
   
    call copy


       
    mov ah, 9
    lea dx, string
    int 21h   
         
   

;-------------------------- 
   


;-------------------------------------------------------------   
   
    mov ax, 4c00h ; exit to operating system.
    int 21h
                       





;---------COPY PROC------------------------------------   
    copy proc
   
    cld
    mov cx, word ptr len
    mov ch, 0   
   
    mov bx, word ptr pos
    mov bh, 0
       
    lea si,buffer
    mov ax, word ptr pos
    mov ah, 0
    inc ax     
    add si,ax   
 

    lea di, string   
 
    rep movsb 
;-------ENDING----
       
     
     mov bx, word ptr len   
     mov bh, 0
     mov string[bx], '$'   
     
     ret   
    copy endp
;-------------------------------------------------------     

;---OUTPUT IN ASCI---------------
printw proc
       
   
    PUSH AX
    PUSH CX
    PUSH DX
    PUSH BX
    PUSH SP
    PUSH BP
    PUSH SI
    PUSH DI
   

    mov cx,0
    mov bx,10
    out_dec1:   
   
    mov dx,0
    div  bx
    push dx
    inc cx
    cmp ax,0
    jne out_dec1
dmet1:
    pop dx
    add dl,30h
    mov ah,2h
    int 21h
    loop dmet1   
   
   pop di
   pop si
   pop bp
   pop sp
   pop bx
   pop dx
   pop cx
   pop ax
 
ret
printw endp                 
;--------------------------------     
   
       
       

                       
end start ;



The problem is that while entering the loop rep movsb, program does nothing with the strings, no action. I see that by watching the variables.
This fact is quite dissapointing me because while writing the program, that procedure worked, at least, it did something with the strings. I created a blank program, and the procedure worked there also. I really see no reason why that's so. Maybe vecause of some flags, or what? I can rewrite it without rep movsb easily, but that's not needed.

Thanks for any help.



;---------COPY PROC------------------------------------   
    copy proc
   
    cld
    mov cx, word ptr len
    mov ch, 0   
   
    mov bx, word ptr pos
    mov bh, 0
       
    lea si,buffer
    mov ax, word ptr pos
    mov ah, 0
    inc ax     
    add si,ax   
 

    lea di, string   
 
    rep movsb 
;-------ENDING----
       
     
     mov bx, word ptr len   
     mov bh, 0
     mov string[bx], '$'   
     
     ret   
    copy endp
;-------------------------------------------------------


PBrennick

Quang,

Welcome to the forum, do not be surprised when a Moderator moves this post to the 16bit programming area.  It is where you will actually get better support but I will try to help you.

The following is causing your problem...

    mov bx, word ptr pos
    mov bh, 0


Setting bh to zero effectively trashes the pointer you were trying to put in BX

Same thing is going on here...
    mov ax, word ptr pos
    mov ah, 0

and here...
    mov cx, word ptr len
    mov ch, 0


Quang, you need to go over the entire program, these errors are everywhere.
hth,
Paul
The GeneSys Project is available from:
The Repository or My crappy website

Quang

I wish it caused the problems, but it's not. I use only the value in AL, so i set zero in AH.

As the matter of fact, the strings stay untouched, whether I do it or not :(

Those things cause nothing. I rewrited the algorithm, using lodsb instead of rep movsb. And it's working.

But why using rep movsb the strings are not executed, I don't know...

MichaelW

For the movs_ instructions the starting source address is DS:SI and the starting destination address is ES:DI.

eschew obfuscation

Quang



  lea si,buffer ; HERE'S THE SOURCE
 
  mov ax, word ptr pos
  mov ah, 0
  inc ax     
  add si,ax   
 
  lea di, string   ; HERE'S THE DESTINATION



I wanna say once more that this procedure works in a blank program perfectly.

MichaelW

I have no idea why it is working in your blank program. The program you posted is an EXE so the loader is setting DS and ES to the segment address of the PSP. Your code is setting DS to your data segment, but leaving ES set to the PSP, so the destination is in the PSP instead of in your data. When you set SI and DI you set only the offset addresses of the source and destination. Try adding a:

push ds
pop  es

at the start of your procedure.

eschew obfuscation

Quang

Had no idea that the problem lies beneath, cos' I read a lot about rep movsb and there wasn't written anywhere about it.

Thanks for knowlege, I owe u much :) Thanks again.