views:

287

answers:

5

In my assembly language class, our first assignment was to write a program to print out a simple dollar-terminated string in DOS. It looked something like this:

BITS 32
    global _main

section .data
    msg db "Hello, world!", 13, 10, ’$’

section .text
_main:
mov ah, 9
mov edx, msg
int 21h
ret

As I understand it, the $ sign serves to terminate the sting like null does in C. But what do I do if I want to put a dollar sign in the string (like I want to print out "it costs $30")? This seems like a simple question, but my professor didn't know the answer and I don't seem to find it using a google search.

A: 

Um. You could write assembly that would taken into account for escaped $, e.g. \$? But then your \ becomes a special symbol too, and you need to use \\ to print a \

Calyth
+7  A: 

You can't use DOS's 0x09 service to display $ signs, you'll need to use 0x02. See here.

codelogic
It looks like 0x40 should work as well: http://www.uv.tietgen.dk/staff/mlha/PC/Prog/asm/int/21/40.htm
Jason Baker
Alternatively, you can use 0x40, but that requires you specify the number of bytes to write (i.e. it doesn't use delimited strings).
Ben Blank
@Jason — Heh, beat me by one second. :-)
Ben Blank
A: 

Try '$$', '\044' (octal) or '\x24' (hex)

Jacek Ławrynowicz
-1: none of these options will solve the problem.
Greg Hewgill
+3  A: 

Or make your own print_string to print a NULL-terminated string using the undocumented INT 29h (print character in AL).

; ds:si = address of string to print
print_string:
    lodsb                   ; load next character from ds:si
    or al, al               ; test for NULL-character
    jz .end_of_string       ; end of string encountered, return.
    int 29h                 ; print character in AL on screen
    jmp print_string        ; print next character
.end_of_string:
    ret                     ; return to callers cs:ip

(Assuming you are using NASM)

Jonas Gulle
Note that if using INT 29h redirection of stdout won't work (e.g test.com > output.txt)
Jonas Gulle
A: 

One way is to find the call that prints a single character. You can print any character with that. Break the string up and print "it costs ", then the '$', and finally, "30". More work, but it gets the job done.

gbarry