The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: williec35 on May 15, 2009, 01:09:50 AM

Title: displaying any register on the screen
Post by: williec35 on May 15, 2009, 01:09:50 AM
Hi, I'm new to MASM32 but I  have program in assembly before( i just know a llittle bit). I'm trying to refresh my self on how  the assembly opcode works by manipulating the general register (eax, ecx,  al, ah, ax..etc).  The  only problem  i have  is  that  i don't  know  how to display the  content  of the register.  how  do i display  eax, ah, al, ecx   ...?  I will appreciated  any help i could  get  Thanks
Title: Re: displaying any register on the screen
Post by: hutch-- on May 15, 2009, 02:22:06 AM
Willie,

Unless you feel like writing integer to string conversions then console display procedures, use the masm32 library and macros to perform this task.

Look at the "print" macro for console display and the "str$" macro to convert the contents of the register to a displayable string. You can alternatively use the direct procedures in the masm32 library and if you want to write the whole lot yourself, the source code for those procedures is available in the m32lib directory.
Title: Re: displaying any register on the screen
Post by: williec35 on May 15, 2009, 02:41:20 AM
Thanks  for  the input.... :)  one  thing  i notice though is that str$ wouldn't display al or ah,  it has  to be a 32 bit  register like  eax.  What would  be the most efficent/easy way  to display just the al or ah register. I'm thinking of creating my own verion  of  the StdOut procedure,   that would some how  mask  out  the eax  register   to get  the  ax,   al  and  ah  component .  What would  be your  opinion  on this.  Again  thanks  for the input  :)
Title: Re: displaying any register on the screen
Post by: MichaelW on May 15, 2009, 03:45:45 AM
You could take the byte or word register and zero-extend (or sign-extend, depending on what you are doing) it into a 32-bit register. For example:

movzx eax, al
print str$(eax)

Title: Re: displaying any register on the screen
Post by: williec35 on May 15, 2009, 01:01:57 PM
Thanks MichaelW .. thats  actually a way better idea!!!!!!!
Title: Re: displaying any register on the screen
Post by: hutch-- on May 15, 2009, 01:33:29 PM
Now if you want to be a real purist so that you don't modify the EAX register try this.


push eax
movzx eax, al
print str$(eax),13,10
pop eax


You push EAX before so its saved on the stack and after you have shown the result restore EAX with the POP so it has not been changed by displaying it.
Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 01:49:07 PM
a real purist would not use str$ at all
he would (i did) write routines to convert to decimal or hex strings and display them
Title: Re: displaying any register on the screen
Post by: UtillMasm on May 15, 2009, 01:53:20 PM
i like your answer!
:cheekygreen: :dance: :toothy
Title: Re: displaying any register on the screen
Post by: williec35 on May 15, 2009, 01:59:21 PM
Thanks hutch  :) ,   Hey david  you  think  you could  post  the  code  you  wrote  for displaying the register... Just  curious  to  see  how  you wrote  it.. I know  you have a nack for  writing  very fast and efficent code, I seen some  of your  posted  comment/codes   :)
Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 02:05:38 PM
i dunno 'bout all that - lol
i like to write code - and i try to make it fast - but most of the guys in here can do better
i am new to the 32-bit world

this rountine converts eax to an ASCII hex string
it could be easily modified for smaller registers
no registers are preserved - just add push/pop as needed
i use the same buffer for 64-bit decimal so it is larger than required
if i wanted to make it faster, i suppose i could unroll the loop and plant 2 or 4 ascii bytes at a time

.data
AscBuf  db        '01234567890123456789',0 ;20 digits

.code
;--------------------------------------------------------

Hex32   proc

;Convert 32-bit binary to ASCII hexidecimal
;
;Call With: EAX= DWORD value to convert
;
;  Returns: EDI= offset into AscBuf of first hexchar
;              = offset AscBuf+12

        std
        mov     edi,offset AscBuf+19
        mov     edx,eax
        mov     ecx,8

Hex32a: and     eax,0Fh
        add     eax,90h
        daa
        adc     eax,40h
        daa
        stosb
        shr     edx,4
        mov     eax,edx
        loop    Hex32a

        cld
        inc     edi
        ret

Hex32   endp

;--------------------------------------------------------
Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 02:45:27 PM
this is what an 8-bit routine might look like....


;--------------------------------------------------------

Hex08   proc

;Convert 8-bit binary to ASCII hexidecimal
;
;Call With: AL= BYTE value to convert
;
;  Returns: EDI= offset into AscBuf of first hexchar
;              = offset AscBuf+18

        mov     edi,offset AscBuf+18
        mov     edx,eax
        and     eax,0Fh
        add     eax,90h
        daa
        adc     eax,40h
        daa
        xchg    eax,edx
        shr     eax,4
        and     eax,0Fh
        add     eax,90h
        daa
        adc     eax,40h
        daa
        mov     ah,dl
        mov     [edi],ax
        ret

Hex08   endp

;--------------------------------------------------------


Title: Re: displaying any register on the screen
Post by: williec35 on May 15, 2009, 04:25:27 PM
Hey Thanks  alot david...  I'm not sure  what "OPTION PROLOGUE:PrologueDef " and   "OPTION EPILOGUE:EpilogueDef" means.
Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 04:35:58 PM
well - the assembler sets up stack frames by default
i don't want (or like or need) the added overhead so i tell it not to
if i want a stack frame, i will set it up myself
but - they may be used in the libraries
so - when i am done - i set the options back to default
(ty Jochen for teaching me)
Title: Re: displaying any register on the screen
Post by: Vortex on May 15, 2009, 05:17:29 PM

include Test.inc

printX MACRO buffer:REQ,_format:REQ,args:VARARG

    invoke  wsprintf,buffer,_format,args
    invoke  StdOut,buffer
   
ENDM

.data

format1 db '%s , X = %d , Y = %d',0
msg     db 'printX test',0

.data?

buffer  db 100 dup(?)

.code

start:

    mov     eax,49152
    mov     ecx,16384
    printX  ADDR buffer,ADDR format1,ADDR msg,eax,ecx
    invoke  ExitProcess,0
   
END start

[attachment deleted by admin]
Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 05:23:49 PM
hey Vortex - the macro library has one that would have done what i want using wsprintf
i tried using udq$ (i think that's the one) but no luck - so i wrote my own 64-bit unsigned to ascii decimal routine
i am too much of a n00b to figure out how to use all the libraries - they aren't always documented
once i wrote that routine, started writing the other ones - they are easy
64-bit to decimal is a little tricky though - lol
Title: Re: displaying any register on the screen
Post by: Vortex on May 15, 2009, 05:28:44 PM
Hi dedndave,

You can find the MASM32 Library Reference in :

\masm32\help\masmlib.chm

Creating your own routines is always a fun and it's a very good method to understand programming concepts.

Title: Re: displaying any register on the screen
Post by: dedndave on May 15, 2009, 05:43:15 PM
truthfully, in the 16-bit world, i had always written my own stuff, anyways
the problem i was having was i had a 64 bit unsigned value in EDX:EAX and wanted to display it in decimal
the documentation was a little unclear as to how to do that
one of the things about wsprintf is, if my code starts looking like C, i want to do it differently - lol
part of the reason i write in assembler to begin with is to have control over things like function overhead
i have dis-assembled programs written in C (many years back)
i know why the apps are so slow - i have seen stuff like this........

SomeStupidProc proc

push bp
mov bp,sp
push ax
mov ax,[bp+4]
inc ax
mov [bp+4],ax
pop ax
pop bp
ret

SomeStupidProc endp

you get the idea (half the time, the value was in ax to begin with)
it seemed a little excessive to me - lol
since then i have written everything in assembler
and i like writing in assembler because if it is slow,
i can isolate the bad guy and try to make it faster