flat assembler
Message board for the users of flat assembler.

flat assembler > Macroinstructions > Macro to display registers

Author
Thread Post new topic Reply to topic
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Does anyone have a macro that will display the values contained in all the following registers, without changing them?

eax
ebx
ecx
edx
esi
edi
ebp
esp
eip
efl
cf
sf
zf
of

This would be very useful for debugging, although I would mostly only use it for the eax, ebx, ecx and edx registers.

_________________
Many things are possible. Few things are likely.
Post 19 Aug 2004, 18:10
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1095
Location: Poland
It would be impossible to do something like this with macros, rather with some run-time procedures. What about just simply running your program under some debugger? Wink
Post 19 Aug 2004, 18:22
View user's profile Send private message Visit poster's website Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
I don't have a debugger right now, and even if I did, I wouldn't use it much. It's much easier to insert something like "display_registers" at problem spots.

_________________
Many things are possible. Few things are likely.
Post 19 Aug 2004, 19:38
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1095
Location: Poland
simple solution that I wrote now (works on windows console app):
Code:
format PE console
entry start
include "%fasminc%/win32a.inc"


section '.code' code readable executable
  start:
        call    display_registers

  finish:
        invoke  ExitProcess,0



display_registers:
        cinvoke printf, szRegs, eax,ebx,ecx,edx,esi,edi,ebp,esp
        retn



section '.data' data readable writeable
 szRegs db "Registers: ", 13, 10
        db "eax: %x", 9, "ebx: %x", 13, 10
        db "ecx: %x", 9, "edx: %x", 13, 10
        db "esi: %x", 9, "edi: %x", 13, 10
        db "ebp: %x", 9, "esp: %x", 13, 10, 0



section '.idata' import data readable
library kernel32,"KERNEL32.DLL",\
        crtdll,"CRTDLL.DLL"

import crtdll,\
       printf,"printf"

include "%fasminc%/apia/kernel32.inc"    


whenever you call "display_registers" function, contents of all regs will be displayed.
Post 19 Aug 2004, 20:06
View user's profile Send private message Visit poster's website Reply with quote
mike.dld



Joined: 03 Oct 2003
Posts: 235
Location: Belarus, Minsk
this might be done by printing to buffer with wsprintfA (user32.dll) and then printing buffer to console, without any crtdll.dll

_________________
http://ostin.googlecode.com/
Post 19 Aug 2004, 20:35
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
coconut



Joined: 02 Apr 2004
Posts: 326
Location: usa
how is it that this works decard, whats %x
Post 20 Aug 2004, 00:10
View user's profile Send private message Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Quote:
It would be impossible to do something like this with macros,


Well, I got the idea from a book on MASM, where they used it as a macro. They didn't give the source code for it, but if it is possible in MASM, I would assume it is also possible in FASM.

But I'm interested in the way you did it. I'm not sure how the .data section works, and I don't know what cinvoke or retn do yet.

Would this work with the GUI?
Post 20 Aug 2004, 04:26
View user's profile Send private message Visit poster's website Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1095
Location: Poland
in FASM it can be implemented with help of macros too (but it isn't actually a macro that displays the contents of registers, macro just calls specified function):
Code:
 format PE console
entry start
include "%fasminc%/win32a.inc"

macro display_registers {
  cinvoke  printf, szRegs, eax,ebx,ecx,edx,esi,edi,ebp,esp
}

section '.code' code readable executable
  start:
        display_registers
        invoke  ExitProcess,0

section '.data' data readable writeable
szRegs db "Registers: ", 13, 10
        db "eax: %x", 9, "ebx: %x", 13, 10
        db "ecx: %x", 9, "edx: %x", 13, 10
        db "esi: %x", 9, "edi: %x", 13, 10
        db "ebp: %x", 9, "esp: %x", 13, 10, 0

section '.idata' import data readable
library kernel32,"KERNEL32.DLL",\
        crtdll,"CRTDLL.DLL"

import crtdll,\
       printf,"printf"

include "%fasminc%/apia/kernel32.inc"    


- data section is simply a place where you declare your data (program's varaibles etc)
- printf function uses C calling convention, that's why you have to use cinvoke instead of invoke;
- retn (actually the same as ret) is "return from procedure" - see some asm tutorial for details.
In GUI it is also possible, although a bit more complicated:
Code:
format PE GUI 4.0
entry start
include "%fasminc%/win32a.inc"

macro display_registers {
  cinvoke sprintf, szBuffer, szRegs, eax,ebx,ecx,edx,esi,edi,ebp,esp
  invoke  MessageBox, 0,szBuffer,szRegisters,MB_OK+MB_ICONINFORMATION
}


section '.code' code readable executable
  start:
        display_registers
        invoke  ExitProcess,0


section '.data' data readable writeable
szRegisters db "Registers", 0

szRegs db "Registers: ", 13, 10
        db "eax: %x", 9, "ebx: %x", 13, 10
        db "ecx: %x", 9, "edx: %x", 13, 10
        db "esi: %x", 9, "edi: %x", 13, 10
        db "ebp: %x", 9, "esp: %x", 13, 10, 0

szBuffer rb 0x100


section '.idata' import data readable
library kernel32,"KERNEL32.DLL",\
        user32,"USER32.DLL",\
        crtdll,"CRTDLL.DLL"

import crtdll,\
       sprintf,"sprintf"

include "%fasminc%/apia/kernel32.inc"
include "%fasminc%/apia/user32.inc"    
Post 20 Aug 2004, 06:56
View user's profile Send private message Visit poster's website Reply with quote
mike.dld



Joined: 03 Oct 2003
Posts: 235
Location: Belarus, Minsk
in addition, you may use %08x to fill number with leading zeroes (or %08X for upper case A..F)

_________________
http://ostin.googlecode.com/
Post 20 Aug 2004, 10:06
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7108
Location: Slovakia
cocount: read description of wsprintf in winapi
Post 20 Aug 2004, 19:38
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
halyavin



Joined: 21 Aug 2004
Posts: 42
I think you should add pushad, pushfd and popfd, popad instruction because WIN32 API (MessageBox) can change registers.
Post 21 Aug 2004, 04:53
View user's profile Send private message Visit poster's website Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Quote:
- data section is simply a place where you declare your data (program's varaibles etc)


No... hehe. I meant the things in the .data section, not the .data section itself. I'm a newbie but I'm not that new! Smile
Post 21 Aug 2004, 06:24
View user's profile Send private message Visit poster's website Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
Anyway, thanks. This was very helpful.
Post 21 Aug 2004, 06:25
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7108
Location: Slovakia
win32 api changes eax (result), ecx and edx, others are preserved.

but i don't know how is it with FPU/MMX/SSE registers
Post 21 Aug 2004, 15:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
halyavin



Joined: 21 Aug 2004
Posts: 42
But we want to preserve all registers, aren't they?
Post 23 Aug 2004, 16:57
View user's profile Send private message Visit poster's website Reply with quote
Imagist



Joined: 13 Jun 2004
Posts: 114
Location: Pennsylvania (USA)
The registers that change during the execution can be pushed onto the stack before they get changed, then popped off afterwards.
Post 23 Aug 2004, 17:59
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7108
Location: Slovakia
halyavin: yes, but we only need to push those 3, others are preserved by called procedure.
Likewise, our procedure has to preserve all registers except those 3. But EBP is preserved in code of "entry" and "return", and eax is result, so you only have to care about EBX, ESI, EDI
Post 23 Aug 2004, 19:52
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.