Displaying symbolic constants in Assembly Language

2.1k views Asked by At

Assuming PrintString is a user defined function that prints a null terminated String, why wouldn't the following code print the value of symbolic constant SecondsInDay? Also any other corrections in the code will be highly welcomed.

.data
SecondsInDay = 60*60*24
textstr TEXTEQU <SecondsInDay>

.code
main PROC
    call Clrscr

    mov  edx,offset SecondsInDay     ; or mov  edx,offset textstr
    call PrintString

    exit
main ENDP

END main
1

There are 1 answers

4
Ross Ridge On BEST ANSWER

Presumably your PrintString function expects EDX to contain an address, one that points to the location in memory where the characters that make up the string to printed are stored. Since you've assigned SecondsInDay a number, the instruction mov edx,offset SecondsInDay loads the EDX register with that number. Not the address of that number, nor the address of a string containing the decimal representation of that number. Since 86400 is probably not a valid address, PrintString will probably crash. Similarly since textstr is a text equate it contains a sequence of characters, which is also not a valid address.

If you want to print out the number of seconds in the day with PrintString you'll first have to allocate storage for the characters that make up the string:

    .data
SecondsInDayStr DB '86400', 0 ; Zero-terminated C-style string

This both provides a location in memory for the string and assigns SecondInDaysStr the address of that location. You can then print it out with PrintString:

     mov  edx, OFFSET SecondsInDayStr
     call PrintString

Note that symbols are things that only the assembler (and maybe the linker and debugger) know about. By the time your program runs all the symbols used by instructions in your code will have been replaced by their actual value. Often symbols are addresses (eg. the symbol main in your program) but neither SecondsInDay nor textstr are addresses.