News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to print out values in MASM syntax?

Started by RedSkeleton007, June 27, 2015, 06:54:38 AM

Previous topic - Next topic

RedSkeleton007

I've put together my first (currently untested) ASM source file. Please take a look at my code. There are some commented questions that describe my troubles:


TITLE Data Definitions (DataDefinitions.asm)
; This program contains data definitions for several data types.
; Each variable is initialized to a value that is consistent with
; its data type.
; Author: RedSkeleton007
; Date Last Modified: 06/26/2015

INCLUDE Irvine32.inc

;What's the characters for asm block comments?
;.data?
;byteValue BYTE ?
;sbyteValue SBYTE ?
;wordValue WORD ?
;swordValue SWORD ?
;sdwordValue SDWORD ?
;fwordValue FWORD ?
;qwordValue QWORD ?
;tbyteValue TBYTE ?
;real4Value REAL4 ?
;real8Value REAL8 ?
;real10Value REAL10 ?
;array WORD 5 DUP(?) ;5 values, uninitialized

.data
;ARRAY
List1 WORD 1,2,3,4,5
List2 SWORD -1,-2,-3,-4,-5
;BYTE
charVariable BYTE 'A'
strVariable BYTE "Green",0 ;5 chars = 5 bytes = 40 bits
numVariable BYTE 0
;SBYTE
sbyte_Var1 SBYTE -128 ;smallest possible signed byte
sbyte_Var2 SBYTE +127 ;largest possible signed byte
;WORD
word_Var1 WORD 65535 ;largest unsigned value
;SWORD
sword_Var1 SWORD -32768 ;smallest signed value
;DWORD
doubleWord DWORD 12345678h ;also can be written as: doubleWord DD 12345678h
;SDWORD
signedDblWord SDWORD -123456789h ;also can be written as: signedDblWord DD -123456789
;FWORD
fword_Var1 FWORD "";48 bits = 6 chars or digits
;QWORD
qword_Var1 QWORD 1234567812345678h
qword_Var2 QWORD "BigFatLazyDude99" ;string of 16 chars in length
qword_Var3 DQ "BigFatLazyDude69" ;string of 16 chars in length
;TBYTE
tbyteNegative TBYTE 800000000000001234h ;decimal value = -1234
tbytePositive TBYTE ? ;what would be the equivalent to the decimal value 1234?
;REAL4
realValue1 REAL4 -1.2
;REAL8
realValue2 REAL8 3.2E-260 ;WTF is this? Scientific notation? Does -260 mean it's signed?
;REAL10
realValue3 REAL10 4.6E+4096

printALL PROTO

main PROC

;call the printALL function to print out all initialized values:
INVOKE printALL

exit
main ENDP

;define the printALL function:
printALL
;Print: charVariable, strVariable, numVariable,...

END


Thanks :biggrin:

dedndave

where is the code for "printAll" ?
or - at least tell us what it's supposed to do

here is a template that may be used with the Irvine32 library
notice that you may have to change the paths for INC's and LIB's

also notice that i have added CaseMap:None
that means that symbol names are case sensitive

;###############################################################################################

        INCLUDE     \Masm32\Irvine\Irvine32.inc  ;adds SmallWin.inc, VirtualKeys.inc

        OPTION      CaseMap:None

        INCLUDE     \Masm32\Irvine\floatio.inc
        INCLUDE     \Masm32\Irvine\GraphWin.inc
        INCLUDE     \Masm32\Irvine\macros.inc

        INCLUDELIB  \Masm32\Irvine\kernel32.lib
        INCLUDELIB  \Masm32\Irvine\user32.lib
        INCLUDELIB  \Masm32\Irvine\Irvine32.lib

;###############################################################################################

        .DATA

szMessage db 'Hello World',13,10,0

;***********************************************************************************************

        .DATA?

;###############################################################################################

        .CODE

;***********************************************************************************************

main    PROC

        mov     edx,offset szMessage
        call    WriteString

        call    WaitMsg
        INVOKE  ExitProcess,0

main    ENDP

;###############################################################################################

        END     main

RedSkeleton007

Quote from: dedndave on June 27, 2015, 07:41:08 AM
where is the code for "printAll" ?
or - at least tell us what it's supposed to do

In my code, I have variables initialized to values under the .data directive. For each of them, I simply want to print out the values they're initialized to. I suppose I could print all of them out one by one in the main PROC. What do you think?

rrr314159

You should put the "printall" function in a separate proc as you indicated. Then, simply write a separate print statement for each datum. You do know how to print one variable, say, a dword?

BTW I'd never heard of an "fword" before. U learn something new every day ... I wonder what format specifier it uses? Probably have to put it in a qword and print that. You have to do similar to print real4 variable with C-style print: put it in a real8. real10 is also difficult - well you can cross those bridges when you get there
I am NaN ;)

dedndave

what i meant was - what are the specific details of the printAll routine?

but - let's be more generic....

let's say you want a PROC that you can INVOKE.....

near the beginning of the program, you need a PROTOtype
it tells masm how many arguments there are, and the arg sizes
for 32-bit code, the sizes are nearly always DWORD (to keep the stack 4-aligned)
usually, PROTO's go right after the INCLUDE's
i often put them in a project INC file - and add it last
but, we're going to demonstrate a single PROTO
MyFunc PROTO :DWORD,:DWORD,:DWORD

next, in the body of the code, the PROC may be called using INVOKE....
    INVOKE  MyFunc,Arg1,Arg2,Arg3
the ArgN values may be registers, memory locations (data symbol names), immediate values, or even referenced addresses
    INVOKE  MyFunc,1,dwMyVariable,eax

now, for the PROC....
following windows ABI, we will preserve EBX, ESI, EDI
EBP is also preserved, but the assembler handles that, as well as the stack frame
MyFunc PROC USES EBX ESI EDI Arg01:DWORD,Arg02:DWORD,Arg03:DWORD

    LOCAL   dwLocalDword       :DWORD  ;create a local dword on the stack
    LOCAL   abArray[256]       :BYTE   ;create a local array of 256 bytes on the stack

    mov     eax,Arg01                  ;for example
;
;
;
    mov     eax,dwLocalDword           ;return result or status in EAX
    ret

MyFunc ENDP

rrr314159

dedndave,

since printALL has no arguments you can simply call it. If u want to invoke it the proto statement is simply as shown by OP. And if you look at his prog he exits after printALL so no need to preserve registers.
I am NaN ;)

jj2007

FWORD exists, but 1. its use is extremely limited, 2. contrary to your expectations, there are five letters, not four, in FWORD 8)

rrr314159

@jj2007,

I thought of the "fword" joke also but refrained from stooping so low :)

Anyway I think OP just needs to know how to do C-style printing with printf, but maybe he wants to know how to invoke ... waiting for further clarification

I am NaN ;)

jj2007


raymond

If Red has  trouble finding a procedure to print out the Real10 format, have a look at the Fpulib if you can use it. You could even use it for ALL your floating point values.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

Tedd

Quote from: RedSkeleton007 on June 27, 2015, 06:54:38 AM
;What's the characters for asm block comments?

COMMENT ~
this is commented out
so is this
and this
~
;anything from this point is not...

The first character after COMMENT marks the start and end -- it can be anything -- in this case ~.

QuotestrVariable BYTE "Green",0 ;5 chars = 5 bytes = 40 bits
5 chars + terminating null = 6 bytes

Quoteqword_Var2 QWORD "BigFatLazyDude99" ;string of 16 chars in length
qword_Var3 DQ "BigFatLazyDude69" ;string of 16 chars in length
Be careful here - the characters are not stored the same way as if you had used "BYTE".

QuotetbyteNegative TBYTE 800000000000001234h ;decimal value = -1234
tbytePositive TBYTE ? ;what would be the equivalent to the decimal value 1234?
1234 = 4D2h
-1234 = FFFFFFFFFFFFFFFFFB2Eh  ;(see two's-complement for representation of negative numbers)

QuoterealValue2 REAL8 3.2E-260 ;WTF is this? Scientific notation? Does -260 mean it's signed?
realValue3 REAL10 4.6E+4096
3.2E-260 = 3.2 x 10-260 = 0.0000000000000.............................00000000032
4.6E+4096 = 4.6 x 104096 = 46000000000000000........................000000000000000
(additional zeroes omitted)
Potato2

RedSkeleton007

Gentlemen, lets start over  :bgrin:
Lets just print out some simple string values, so that I can learn what MASM syntax is equivalent to "cout << stringValue << endl" in C++.
Here's my untested code:

; This program defines symbolic constants for all seven days
; of the week, then demonstrates them by outputting them to
; the screen.

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
numDays BYTE "There are 7 days in a week. They are:"
     
     Mon BYTE "Monday",0
     Tues BYTE "Tuesday",0
     Wed BYTE "Wednesday",0
     Thurs BYTE "Thursday",0
     Fri BYTE "Friday",0
     Sat BYTE "Saturday",0
     Sun BYTE "Sunday",0

.code
main proc


     ; What's the MASM syntax for outputting values stored in variables?
     
     ; output Mon,0dh,0ah,0
     ; output Tuesday,0dh,0ah,0
     ; output Wednesday,0dh,0ah,0
     ; output Thurs,0dh,0ah,0
     ; output Fri,0dh,0ah,0
     ; output Sat,0dh,0ah,0
     ; output Sun

invoke ExitProcess,0
main endp
end main


For my intentions, the C++ equivalent of what I'm trying to accomplish here is:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <array>
#include <string>

using namespace std;

int main()
{
     string Mon = "Monday";
     string Tues = "Tuesday";
     string Wed = "Wednesday";
     string Thurs = "Thursday";
     string Fri = "Friday";
     string Sat = "Saturday";
     string Sun = "Sunday";

     cout << "There are 7 days in a week. They are: \n";
     cout << Mon << endl;
     cout << Tues << endl;
     cout << Wed << endl;
     cout << Thurs << endl;
     cout << Fri << endl;
     cout << Sat << endl;
     cout << Sun << endl;

     system("pause");
     return 0;
}


Also, why is the BYTE intrinsic data type used so often?

Vortex

I would adopt a simple way to use the streams :

include        Streams.inc

_iobuf STRUCT
    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?
_iobuf ENDS

FILE TYPEDEF _iobuf

.data

msg     db 'stdout = %X',0

.data?

stdout  dd ?
stdin   dd ?
stderr  dd ?

.code

start:

    call    crt___p__iob
    mov     stdin,eax           ; #define stdin  (&__iob_func()[0])

    mov     ecx,SIZEOF(FILE)

    add     eax,ecx
    mov     stdout,eax          ; #define stdout (&__iob_func()[1])

    add     eax,ecx
    mov     stderr,eax          ; #define stderr (&__iob_func()[2])

    invoke  crt_fprintf,stdout,ADDR msg,stdout
           
    invoke  ExitProcess,0

END start



dedndave

DAYS_OF_WEEK STRUCT
  lpSun LPSTR ?
  lpMon LPSTR ?
  lpTue LPSTR ?
  lpWed LPSTR ?
  lpThu LPSTR ?
  lpFri LPSTR ?
  lpSat LPSTR ?
DAYS_OF_WEEK ENDS

    .DATA
    ALIGN   4

dow DAYS_OF_WEEK <szSun,szMon,szTue,szWed,szThu,szFri,szSat>

szSun BYTE "Sunday",0
szMon BYTE "Monday",0
szTue BYTE "Tuesday",0
szWed BYTE "Wednesday",0
szThu BYTE "Thursday",0
szFri BYTE "Friday",0
szSat BYTE "Saturday",0


now, you can calculate the offset into the "dow" structure using (4*X), where X = 0 to 6

;EAX = 0 to 6

    mov     edx,dow[4*eax]   ;EDX = address
    print    edx


you can certainly do that in a single line, too

    print   dow[4*eax]

if you just want to print a string....

    print   offset szMon

the print macro wants an address

jj2007

One more  :biggrin:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  For_ ecx=0 To 6
      PrintLine fDate$(ecx, "dddd")
  Next
  Inkey "(wow, that was tough)"
EndOfCode


Output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday


Will work only today 8)