News:

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

String copying (irvine) help

Started by j00n, October 30, 2011, 09:18:58 AM

Previous topic - Next topic

j00n

the part that says target[esi] is the same as target+0 or [target+0] or target+edx (if incremented and preserved) ?

using target[esi]   (esi is just used for counting in this example?) so edi could have been used as well?

this is the first time i've seen target[esi] and wondered how else it could be used...

array[index] - like this? (but for index only registers can be used here right?)

sorry for all the mashed up questions i'm trying to understand it fully

include \masm32\include\masm32rt.inc

; al (8 bits) (1 byte)
; ax (16 bits) (2 bytes)
; eax (32 bits) (4 bytes)

TITLE Copying a string

.data

source BYTE "This is the source string",0
target BYTE SIZEOF source dup(0)

.data?
     
.const

.code

start:

main proc
   
    mov esi, 0
    mov ecx, SIZEOF source

L1:
    mov al, source[esi]     ; move each BYTE to low byte of ax
    mov target[esi],al      ; mov each BYTE from low byte of ax to target
    inc esi                 ; load next byte until target = source
LOOP L1                     ; LOOP auto decrements ecx


    inkey "Press any key to continue."

    ret
    main endp
   
end start

dedndave

yes - ESI is an index and can be used for arrays
you can also combine a base and an index register to access an array as 2-dimensional
        mov     al,ByteArray[ebx+esi]

for the code you have above, you could use string instructions
this code copies the contents of [source] to [target]
        mov     esi,offset source
        mov     edi,offset target
        mov     ecx,sizeof source
        rep     movsb

it is smaller and faster

when done....
ECX will be 0
ESI will point to the byte after the end of "source"
EDI will point to the byte after the end of "target"

http://www.masm32.com/board/index.php?topic=4083.msg148040#msg148040