News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Declaring a string and using it to write to console

Started by csimpson, April 14, 2005, 07:30:43 PM

Previous topic - Next topic

csimpson

I have been trying to declare a string and then use it to write to the console.

The program is the following:

...

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .data
        arr db "Hello World!"
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:
    print arr                                   ;write to console
    mov eax, input("Press enter to exit...")    ;pause
    exit
end start


Brett Kuntz

You can find Console related API here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp

You can use:

BOOL WriteConsole(
  HANDLE hConsoleOutput,
  const VOID* lpBuffer,
  DWORD nNumberOfCharsToWrite,
  LPDWORD lpNumberOfCharsWritten,
  LPVOID lpReserved
);

To do a lot of basic console output, ie:

arr db "Hello", 0
hHandle HANDLE 0

invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hHandle, eax
...
invoke WriteConsole, hHandle, addr arr, 5, 0, 0


I think that'll help you out. Also, add a , 0 to your .data:

.data
        arr db "Hello World!", 0

pbrennick

csimpson,
The missing '0' is all that you need to fix in your code as long as you are declaring the macros you are using.  It is a real pain trying to help someone when you only get to see what they want you to see, be advised.  If you want the best help, always attach the project.

Paul

dsouza123

Three versions of console output and input,
figuring out how to use a sz (zero terminated string)
in the .data area with the print macro was the hurdle.
Use   print  offset szMessage


    .586
    .model flat, stdcall
    option casemap :none

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\user32.inc
    include \masm32\macros\macros.asm

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\user32.lib

    .data
      lpszBuf1   dd 0
      lpszBuf2   dd 0
      szMsg      db "3 Hello World!",0
      lpszMsg    dd 0
      szPrompt   db "3 Press enter to exit...",0
      Buf3       db 128 dup (0)
      lpszBuf3   dd 0
     
    .code
start:
    ;-------------------- Version 1
    print "1 Hello World!"
    mov eax, input("1 Press enter to exit...")
    mov lpszBuf1, eax

    ;-------------------- Version 2
    ; mov eax, offset szMsg   
    ; mov lpszMsg, eax
    ; print lpszMsg
    ;----------------- Alternative to preceding 3 lines
    print offset szMsg
   
    mov eax, input("2 Press enter to exit...")
    mov lpszBuf2, eax

    ;-------------------- Version 3
    invoke StdOut, ADDR szMsg
    invoke StdOut, ADDR szPrompt
    invoke StdIn,  ADDR Buf3, LENGTHOF Buf3
    invoke StripLF,ADDR Buf3
    lea eax, Buf3
    mov lpszBuf3, eax
    ;----------------- Alternative to   lea eax, Buf3
    mov eax, dword ptr Buf3
    mov lpszBuf3, eax

    exit
end start

hutch--

csimpson,

The "print", "input" and "exit" macros are designed to make console coding faster and more convenient but they have the source available so you can understand how they work. Paul's comment is useful to you in that normal string data in Windows is zero terminated. So you string displays correctly at the console, try this,


  txt db "hi folks",13,10,0


The "print" macro always appends the terminating zero and if you have a later version of the "print" macro, you can use this form,


.data
    txt db "Hi Guys",0

.code
    ........
    print txt,13,10
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

csimpson,

The print macro expects the address of a zero-terminated string.

print addr arr
print SADD("Hello World!")  ; SADD must be upper case
print chr$("Hello World!")

eschew obfuscation

csimpson

Thank You ALL for your help in such a short time.  I'm new to Masm.  I hope I can return the favor in the future.

csimpson

askm

And how would you,
to the console,
or even to another string,
write BOTH the name and value by a macro
just by passing a (.data) name ?

.data
var_name db "Forum",0

.code
...
macro_nameandvalue var_name
...

might output (eg, with 13,10 between them):

var_name
Forum

In advance thanks.


askm

I found that something like

xvar MACRO var_name:DWORD
mstr BYTE "&var_name",0
print mstr

;  or this,
;print chr$(mstr)
;  depends on what passed
;  then just

print chr$(13,10)
print str$(var_name)

;  may do what we need

endm

jj2007

Some more valid options:

.data
AppName db "Test app",0
.code
print "Ciao bello",13,10
print chr$("How is life?",13,10)
print chr$("How is life?"),13,10
print cfm$("This is a\nnew line"),13,10
print offset AppName, 13,10
print addr AppName, 32
print str$(12345),13,10


As you see, this macro is very versatile. Under the hood, however, it works with pointers to addresses...