;============================================================
;Module:dumpreg.asm
;created: 07/26/2016
;By: John Endler
;Editor:Vim
;
;Copyright John Endler - MIT License
;
;About: Program dumps to screen contents of cpu registers
;       EAX, EBX, ECX, EDX, ESI, EDI
;
;Version: 0.0.3
;
;You can use in Assembly program to check contents of registers
;at selected point in your program.
;
;FASM 1.71.54
;  
;NOT A STAND ALONE PROGRAM
;
;USE: Include in ASM program: include 'dumpreg.asm'
;     Call from inside your program with: call ShowReg
;
;============================================================

STDOUT      equ 1
SYS_WRITE   equ 4
    ;-----------------------
    ;Takes 2 arguments
    ;1=addr of string
    ;2=length of string
    ;-----------------------
    macro  StdOut opt1,opt2
    {
        mov eax,SYS_WRITE
        mov ebx,STDOUT
        mov ecx,opt1
        mov edx,opt2
        int 80h
    }


segment readable executable

;=================================================
;               MODULE PROCEDURES 
;=================================================

ShowReg:


;show EAX    
    call DwordToHex
    pushad
    StdOut eaxreg,eaxreg_l
    StdOut reghex,reghex_l
    popad
;show EBX
    mov eax,ebx
    call DwordToHex
    pushad
    StdOut ebxreg,ebxreg_l
    StdOut reghex,reghex_l
    popad
;show ECX
    mov eax,ecx
    call DwordToHex
    pushad
    StdOut ecxreg,ecxreg_l
    StdOut reghex,reghex_l
    popad
;show EDX
    mov eax,edx
    call DwordToHex
    pushad
    StdOut edxreg,edxreg_l
    StdOut reghex,reghex_l
    popad
;show ESI
    mov eax,esi
    call DwordToHex
    pushad
    StdOut esireg,esireg_l
    StdOut reghex,reghex_l
    popad
;show EDI
    mov eax,edi
    call DwordToHex
    pushad
    StdOut edireg,edireg_l
    StdOut reghex,reghex_l
    popad

    ret

;=================================================
;               CONVERSION PROCEDURE
;=================================================

;=================================================
;Convert dword from register to hex digits
;
;Input:  dword to covert in EAX
;Output: eaxhex in DATA Segment
;
DwordToHex:
    push ecx
    push edi

    cld                            ;clear direction flag for stosb
	mov edi,reghex                 ;save converted hex here
	mov ecx,28                     ;start at top 4 bits in eax
@@:
    push eax    	               ;save dword in eax for conversion
	shr eax,cl 		               ;get 4 bits
    and eax,0000000Fh              ;use lower 4 bits
	mov al,[hextable+eax]          ;convert to hex
	stosb			               ;save byte from al to edi inc edi
	pop eax			               ;restore saved dword
    sub ecx,4                      ;get next 4 bits      
    test ecx,ecx                   ;are we on last 4 bits
    jnz @b

    push eax                       ;save dword  
    and eax,0000000Fh              ;get last 4 bits
    mov al,[hextable+eax]
    stosb
    pop eax                        ;restore saved dword 

	pop edi
    pop ecx
	ret	

;hex table used to convert nibble to hex digit 	
	hextable:  db '0123456789ABCDEF'

;=================================================
;           INITIALIZED DATA
;=================================================

eaxreg   db  0ah,'EAX = ',0
eaxreg_l = $-eaxreg

ebxreg   db  'EBX = ',0
ebxreg_l = $-ebxreg

ecxreg   db  'ECX = ',0
ecxreg_l = $-ecxreg

edxreg   db  'EDX = ',0
edxreg_l = $-edxreg

edireg   db  'EDI = ',0
edireg_l = $-edireg

esireg   db  'ESI = ',0
esireg_l = $-esireg

segment readable writeable
;==================================================
;           UNINITIALIZED DATA
;==================================================

reghex   db '00000000',0Ah,0
reghex_l = $-reghex
