Author Topic: Strange text characters displayed before string.  (Read 1968 times)

xm00x

  • Guest
Strange text characters displayed before string.
« on: June 19, 2013, 12:56:03 PM »
When I assemble and link(16) the following code below, the output has some strange junk characters before the main string.  I am assembling under Windows 7 with the commands:

ml /AT /c hello.asm
link16 hello.asm

Code: [Select]
.model tiny

.code

ORG 100H
HOST:
mov ah,9
mov dx,OFFSET HI
int 21H

mov ax,4C00H
int 21H

HI DB "Hello hello hello!$"

END HOST


Output, after 3 new lines, is:   3Ê┤      ║♫☺═!© L═!Hello hello hello!

Many thanks!

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Strange text characters displayed before string.
« Reply #1 on: June 19, 2013, 05:04:27 PM »
Interrupt 21h Function 9 expects the string to be at DS:DX. When you linked without /TINY the linker produced a 16-bit EXE file, and when the loader loads a 16-bit EXE it leaves the DS and ES segment registers set to the segment address of the PSP. For a 16-bit EXE the PSP segment precedes the program segment in memory, so the function started displaying at the offset address of the string, as calculated from the start of the program, but in the PSP segment, and displayed everything between the starting point and the ‘$’ terminator.

If you had linked it with /TINY the linker would have produced a .COM file and the loader would have set all of the segment registers to the segment address of the PSP. And since for a .COM file the PSP is effectively part of the program, and you allowed room for it in the program address space with the ORG 100H, DS:DX would have pointed to the start of the string, and it would have displayed as expected.
« Last Edit: June 19, 2013, 06:09:42 PM by MichaelW »
Well Microsoft, here’s another nice mess you’ve gotten us into.

dedndave

  • Member
  • *****
  • Posts: 8749
  • Still using Abacus 2.0
    • DednDave
Re: Strange text characters displayed before string.
« Reply #2 on: June 19, 2013, 07:38:11 PM »
your code is ok, but i use the following "template" for tiny model programs
Code: [Select]
        .MODEL  Tiny
        .386
        OPTION  CaseMap:None

;####################################################################################

        .CODE

;************************************************************************************

        ORG     100h

_main   PROC    NEAR

        mov     dx,offset s$string
        mov     ah,9
        int     21h
        mov     ax,4C00h
        int     21h

_main   ENDP

;************************************************************************************

s$string db 'Hello hello hello!',0Dh,0Ah,24h

;####################################################################################

        END     _main

in addition to what Michael said about using the /TINY switch with Link16.exe,
i would also use the "/omf" switch with ML.exe
Code: [Select]
ml /omf /c hello.asm
link16 /TINY hello.asm,hello.com;
on most versions of MASM, /omf implies /c, and /omf is probably the default
but - the switch behaviour varies a little from version to version   :P
in some cases, the switches are case-sensitive - i think /omf is one of those
« Last Edit: June 20, 2013, 02:48:02 AM by dedndave »

xm00x

  • Guest
Re: Strange text characters displayed before string.
« Reply #3 on: June 19, 2013, 10:28:16 PM »
Many thanks for the quick replies, guys, and the detailed info.   :t

I have been using this forum a lot of late, and hope to use it more.  Great community you got going on here.   :eusa_dance: :eusa_clap: