Why does this code print out junk and make the computer beep?

Started by Matthew Curtis, May 08, 2008, 09:53:24 AM

Previous topic - Next topic

Matthew Curtis

I am writing this code just to get used to using functions in masm but I keep getting weird output in addition to my expected output.

output


c:\masm32\myfiles\hello>hello
═  ƒ Ü≡■↔≡▐☺AK☺AV☺AA☺☺☺ ☻               ⌡◄Σ♥à↕¶ ↑ c↕        ♣               ═!╦

             Hello



c:\masm32\myfiles\hello>


Here is the code. I'm not sure what I'm doing exactly, I keep checking the reference for things like the .startup directive but I'm not finding anything.
Thanks ahead of time for any help you can give. Wracking my brain trying to figure this out


.MODEL TINY
.286
.stack

.code

main PROTO
displayOutput PROTO

MESSAGE db "Hello"

.startup

call main

main proc

invoke displayOutput

mov ah, 0 ; Function 0 of INT 16
int 16h ; Wait for a keypress

mov ax, 4c01h ; Function 4C01 of INT 21
int 21h ; Normal Program Termination

main endp

displayOutput proc

mov dx, OFFSET MESSAGE ; Offset of our Message
mov ah, 9 ; Function 9 of INT 21
int 21h ; Display $-String
jmp ToEnd ;

displayOutput endp

ToEnd:

End



hutch--

Vaguely from memory the DOS function 09h requires a "$" terminator so you don't get the junk after it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Matthew Curtis

I changed that and it did get rid of all the extra spaces afterwards, however it still prints out those strange ascii characters.
I kinda wonder if some sort of buffer needs to be flushed.

evlncrn8

 jmp ToEnd ;

displayOutput endp

ToEnd:

------------------------------------

you have no return

ToEnd:

mov ax, 4c00h
int 21h


so the characters you see is crap, you're lucky you didn't crash....


[here's the fixed code]

main proc

invoke displayOutput

mov ah, 0 ; Function 0 of INT 16
int 16h ; Wait for a keypress

mov ax, 4c01h ; Function 4C01 of INT 21
int 21h ; Normal Program Termination

main endp

displayOutput proc

mov dx, OFFSET MESSAGE ; Offset of our Message
mov ah, 9 ; Function 9 of INT 21
int 21h ; Display $-String
ret

displayOutput endp

End


MichaelW

If you are directing the linker to produce a COM file, then the problem is the placement of your string definition relative to the startup directive. For a COM file the major function of the startup directive is to set the location counter to 100h to allow room for the program PSP. Because you are defining the string above the startup directive, at an address that will be occupied by the PSP at run time, OFFSET MESSAGE will return 0, instead of the offset address of the string. If you create a data segment and define the string there, or define the string in the code segment below the startup directive, and outside the execution path so it will not be "executed" as if it were code, then the program should correctly display the string.

If you are directing the linker to produce an EXE file, then your code has two problems that will prevent the string from displaying correctly. DOS function 09h expects the string to be at the address specified by DS:DX. Because of the TINY memory model the startup directive is expecting the program loader to set DS to the segment address of the program's data, but for an EXE file the startup code must do this. Since the program's startup code does not, at run time DS will be set to the segment address of the PSP, instead of to the segment address of the string. Because the string is defined in the code segment OFFSET MESSAGE will return the offset address of the string relative to the code segment, instead of relative to the data segment as the DOS function expects.

The beep occurs when the DOS function displays ASCII code 7.
eschew obfuscation