Search code examples
stringassemblytasm

Manipulate string in assembly x86 (mov and print to screen)


I'm working on a larger project but I'm stuck with string manipulation. My assembly file includes math coprocessor operations (it starts the coprocessor with "FINIT") but I don't think it should be interfering at all. Basically, I have some strings that are 50 bytes long each:

$s db 50 dup (?), '$'
_cte_14 db "hello world", '$', 39 dup (?)

I need to assign the value stored in variable "_cte_14" to variable "$s" I attempted to use a register to temporarily store the value, like this:

mov cx, _cte_14
mov $s, cx

but I get the "operand types do not match" error.

Since I know the AX, BX, CX, DX registers only hold 16 bits, I thought maybe I need to work with the memory address of the first string character, so I tried:

mov bx, offset _cte_14
mov $s, bx

but the same error shows up.

I'm using TASM to compile for an x86 processor. What would be the right way to accomplish this?

Thanks a lot in advance.


Solution

  • Example for to copy the characters in a loop:

    s db 51 dup ('$')
    _cte_14 db "hello world"
    len = ($ - _cte_14)    ; (current location - offset _cte_14)
    40 dup ('$')
    
    mov si, offset _cte_14 ; get source offset
    mov di, offset s       ; get destination offset
    mov cl, len            ; length of the string
    P1:
    mov al, [si]           ; get byte from DS:SI
    mov [di], al           ; store byte to DS:DI
    dec cl                 ; decrease counter, set zero flag if zero
    jnz P1                 ; jump if zero flag is not set
    

    -- Variation with using a string instruction together with a repeat instruction prefix:

    mov si, offset _cte_14
    mov di, offset s
    mov cx, len ; length of the string
    cld         ; clear direction flag
    rep movsb   ; copy from DS:SI to ES:DI, increase SI+DI, decrease CX, repeat CX times