Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Assembly8086 how to put edit a string(db)

Options
  • 28-10-2006 6:30pm
    #1
    Registered Users Posts: 695 ✭✭✭


    Im new to assembly and wondering how to put stuff into a string at run time. Actually first off am I right in calling it a string? Heres a quick idea of what I mean:
    .model small
    .stack
    .data
    strMSG db 'Hello','$'
    .code
    mov ax,@data
    mov ds,ax
    
    [COLOR="Blue"]mov strMSG,'World'[/COLOR]
    
    mov ah,4ch
    int 21h
    end
    

    The part in blue is what im unsure how to do.
    How could I add the word "World" to the string "Hello"?

    Thanks


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    What you mean here is how do you Concatenate two strings in x86.

    I amn't that familer with x86 but this is taken from a Book:
    The strcat comes from the UCR library.

    The strcat(xx) routines perform string concatenation. On entry, es:di points at the first
    string, and for strcat/strcatm dx:si points at the second string. For strcatl and strcatlm the second
    string follows the call in the code stream. These routines create a new string by
    appending the second string to the end of the first. In the case of strcat and strcatl, the second
    string is directly appended to the end of the first string (es:di) in memory. You must
    make sure there is sufficient memory at the end of the first string to hold the appended
    characters. Strcatm and strcatml create a new string on the heap (using malloc) holding the
    concatenated result. Examples:
    String1 byte “Hello “,0
    byte 16 dup (0) ;Room for concatenation.
    String2 byte “world”,0
    ; The following macro loads ES:DI with the address of the
    ; specified operand.
    lesi macro operand
    mov di, seg operand
    mov es, di
    mov di, offset operand
    endm
    ; The following macro loads DX:SI with the address of the
    ; specified operand.
    ldxi macro operand
    mov dx, seg operand
    mov si, offset operand
    endm
    .
    .
    .
    lesi String1
    ldxi String2
    strcatm ;Create “Hello world”
    jc error ;If insufficient memory.
    print
    byte “strcatm: “,0
    puts ;Print “Hello world”
    putcr
    free ;Deallocate string storage.
    .
    .
    .
    lesi String1 ;Create the string
    strcatml ; “Hello there”
    jc error ;If insufficient memory.
    byte “there”,0
    print
    byte “strcatml: “,0
    puts ;Print “Hello there”
    putcr
    free
    .
    .
    .
    lesi String1
    ldxi String2
    strcat ;Create “Hello world”
    printf
    byte “strcat: %s\n”,0
    .
    .
    .
    ; Note: since strcat above has actually modified String1,
    ; the following call to strcatl appends “there” to the end
    ; of the string “Hello world”.
    lesi String1
    
    strcatl
    byte “there”,0
    printf
    byte “strcatl: %s\n”,0
    


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    DaSilva wrote:
    The part in blue is what im unsure how to do.
    How could I add the word "World" to the string "Hello"?

    Thanks
    .model small
    .stack
    .data
    strMSG db 'Hello','$'
    .code
    mov ax,@data
    mov ds,ax
    
    mov strMSG,'World'
    
    mov ah,4ch
    int 21h
    end
    

    you could do this, assuming you're using tasm ideal mode.
    .model small
    .stack
    
    .data
    strHello db 'Hello, ',0
    strWorld db 'World',0
    
    buffer   db   128   dup  (0)
    
    .code
       mov ax,@data
       mov ds,ax
       
       lea si,[strHello]
       lea di,[buffer]
       call poor_strcpy
    
       lea si,[strWorld]
       lea di,[buffer]
       call poor_strcat
    
       mov cx,ax           ; assuming a string was concat, length in ax->cx
       lea dx,[buffer]     ; data to write in dx
       xor bx,bx             ; stdout?
       mov ah,40h         ; write data
       int 21h               
       
       mov ah,4ch
       xor al,al
       int 21h
    ;================  
    poor_strlen:
       push di
       or cx,-1                       ; init to 0ffffh
       xor ax,ax                      ; scan for null byte
       repnz scasb            
       not cx                          ; invert to get length
       pop di                          ; restore di
       ret
    ;================   
    poor_strcat:
       call poor_strlen                ; get length of string at di
       jcxz end_strcat              ; if its zero, exit
       mov ax,cx                       ; save in ax for return
       rep movsb                       ; move bytes from si->di
    end_strcat:
       ret
    ;================
    poor_strcpy:
       lodsb                         ; load a byte from si
       stosb                         ; save in buffer at di
       or al,al                       ; check if null byte, end of string
       jnz poor_strcpy            ; keep copying bytes if not zero
       ret
    

    if you're printing dollar terminated strings, you use 09h but output date to stdout handle, then 40h with length in cx & either 1 or 0 in bx..can't remember now.


Advertisement