The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ookami on February 21, 2011, 01:34:22 PM

Title: print chr$
Post by: ookami on February 21, 2011, 01:34:22 PM
1. I saw many times :

print chr$ ("hello world")

But

print "hello world"

works perfectly. So why bother with the chr$ macro to display a string ?

2. Another question : how to display a string stored in memory like this one :

hello   byte   "hello world !"

3. And is that the right way to store a string in memory, or there's another one.

Thank you.
Title: Re: print chr$
Post by: dedndave on February 21, 2011, 02:25:20 PM
        .DATA

hello   db 'Hello World',0

        .CODE

        print   offset hello

you could also
        mov     eax,offset hello
        print   eax

i didn't know the print macro would do that - it probably uses chr$, though   :U

the chr$ macro creates a string similar to the one we defined, and passes the address
the advantage is - you can see the text inline
the disadvantage is - you only get that address once - with the DB method, you can access the label as many times as you like
Title: Re: print chr$
Post by: brethren on February 21, 2011, 03:41:59 PM
the chr$ macro is used to create a string with multiple elements

mov ecx, chr$("hello", 13, 10, "world", 13, 10)

is the same as

.data
str1 BYTE "hello", 13, 10, "world", 13, 10, 0
.code
mov ecx,  OFFSET str1
Title: Re: print chr$
Post by: jj2007 on February 21, 2011, 04:55:09 PM
print "Hello", 13, 10 is also valid syntax, i.e. you can append a CrLf that way.
Title: Re: print chr$
Post by: Shooter on February 21, 2011, 05:10:09 PM
lea eax, hello

is the same as

mov eax, offset hello

Not sure which is better though, I haven't studied it that in depth before.
Title: Re: print chr$
Post by: dedndave on February 21, 2011, 05:17:07 PM
LEA is generally used if the address is on the stack, however it has other uses
MOV is smaller or faster - i forget which - lol

LEA calculates and Loads the Effective Address
if the address is a constant, there is no need to do all that - just use MOV

LEA can also be used to make calculations without altering the flags
        lea     edi,[edi+4]  ;adds 4 to EDI
it is used for other similar tricks, too
Title: Re: print chr$
Post by: Shooter on February 21, 2011, 05:47:50 PM
I think it was mostly due to the flags that our instructor (whew! almost 20 years ago) suggested we use LEA... at that time we used the flags for quite a number of conditionals.
Title: Re: print chr$
Post by: dedndave on February 21, 2011, 06:00:43 PM
well - it is an easy way to avoid problems with local variables, too
if you try to use MOV with OFFSET of a local, it spits out an error, of course
knowing when to use LEA/MOV or ADDR/OFFSET is a little like learning to balance the stack   :P
Title: Re: print chr$
Post by: Shooter on February 21, 2011, 06:41:49 PM
Quote from: dedndave on February 21, 2011, 06:00:43 PM
knowing when to use LEA/MOV or ADDR/OFFSET is a little like learning to balance the stack   :P

I really need to find and break out some books and re-study those little things. I miss my QUE books for advanced assembly.  :(