Search code examples
assemblyx86-16dostasm

Move string to end of file assembly


I need to move my message string to the end of program and the program need to output it correctly. How can I do that?

.model small
.stack 100h

.data

.code
main:
    mov ax, @data
    mov ds, ax

output:
    jmp message
    MOV dx, offset message
    mov ah, 09h
    int 21h

    mov ah, 4Ch
    int 21h


message:
 db 'Hello, World!', '$'

end main


Solution

  • I need to move my message string to the end of program and the program need to output it correctly.

    The way I understand this is, that you don't want your message string to be part of the .data section, but rather reside in the .code section.
    Your current solution (?) jmp message can't help you as it will bypass the one thing that can output your string, and worse, it will begin executing garbage instructions formed by whatever bytes happen to be in the message string.
    The jmp message clearly has to go, but there is one change that is required. The DOS.PrintString function 09h always expects a far pointer to the $-terminated string in the registers DS:DX, so you need to copy CS to DS since the string now resides in the code segment. That's all!

    main:
        push cs
        pop  ds
        mov  dx, offset message
        mov  ah, 09h
        int  21h
    
        mov  ax, 4C00h
        int  21h
    
    message: db 'Hello, World!$'
    
    end main