The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Zap on May 27, 2009, 08:56:46 PM

Title: to output a string from inside a proc
Post by: Zap on May 27, 2009, 08:56:46 PM
Hi,guys!
Here`s two ways to output "12345",that`s all of the string  from inside a proc.
How do i output "345",that`s part of the string from inside of the proc? Have tried all kinds of combinations but cant work it out.


lpCL            BYTE   "12345",0
-----------
MsgBox "%s",  addr lpCL+2            ;will output "345",need to do the same from inside of the proc

invoke f,addr lpCL

------------
f PROC  dCL:DWORD

MsgBox "%s",  dCL         ; way 1 to   output "12345"                                           
----
mov eax,dCL
MsgBox "%s", eax          ; way 2  to   output "12345"
---
                                  ;how do i  get "345"?
ret
f endp
---------------

Thanx.
Title: Re: to output a string from inside a proc
Post by: PBrennick on May 27, 2009, 09:01:12 PM
In the masm32 macro section, search for and examine the 'InString' macro (or just use it).  :U

'nuff said,
Paul
Title: Re: to output a string from inside a proc
Post by: redskull on May 27, 2009, 10:39:26 PM
you have to deference it first:

mov eax, dCL  ; move the address of the address into eax
mov eax, [eax] ; move the pointer into eax
MsgBox  "%s", eax

Though it's not very good programming practice, technically you can reference the global variable with no problem from inside the function as well (MsgBox "%s", lpCL+2).  Also, be warned, invoke often doesn't play well with arguments in eax, so you might need to a different register

-r


Title: Re: to output a string from inside a proc
Post by: jj2007 on May 28, 2009, 12:05:03 AM
Here are some options.

include \masm32\include\masm32rt.inc

f1 PROTO: DWORD
f2 PROTO: DWORD

.data
lpCL            BYTE   "12345",0

.code
start:
MsgBox 0, addr lpCL+2, "Outside", MB_OK

invoke f1, addr lpCL
invoke f2, addr lpCL+2 ; addr is the same as...

invoke f1, offset lpCL
invoke f2, offset lpCL+2 ; ... offset for GLOBAL variables

exit

f1 proc  dCL:DWORD
mov eax, dCL
add eax, 2
MsgBox 0, eax, "Inside f1", MB_OK

; MsgBox 0, dCL+2, "Inside f2", MB_OK ; would assemble but crashes

ret
f1 endp

f2 proc  dCL:DWORD
MsgBox 0, dCL, "Inside f2", MB_OK
ret
f2 endp

end start
Title: Re: to output a string from inside a proc
Post by: Zap on May 28, 2009, 03:24:33 PM
Thanx a lot,guys.
This pointer/(de)referencing stuff is pretty confusing for a newbie.
i`m sure u`ve been there :)
Title: Re: to output a string from inside a proc
Post by: PBrennick on May 28, 2009, 06:27:01 PM
Zap,

Dereferencing is a complicated term for a simple task. In Motorola nomenclature we used to refer to that as indirect referencing which is a lot more descriptive of what is actually happening. An Indirect Reference is a reference to a reference.

Paul
Title: Re: to output a string from inside a proc
Post by: Zap on May 28, 2009, 08:35:30 PM
 :dazzled:
Title: Re: to output a string from inside a proc
Post by: redskull on May 28, 2009, 10:20:00 PM
My bad everyone; as was pointed out (pun intended), my code is erroneous and the extra deferencing is not required in this case.  Sorry to muddy the waters further

-r