Register Dump

VSBrown 0 Tallied Votes 2K Views Share

Will demonstrate dumping the registers.

TITLE: Register Dump (RegDump.asm)

;------------------------------------------------------------
; Name: 
;
; Filename: RegDump.asm
;
; Project #: 2
;
; Completed: February 09, 2003
;
; Description: My second assembly program. Will demonstrate
;              dumping the registers. Demonstrates problems
;              from Assembly Language For Intel-Based Computers,
;              4th Ed. by Kip R. Irvine. Problems are #1 on pg.
;              96, #1 on pg. 133 and #7 and #8 on pg. 134.
;
; Reference:   Assembly Language For Intel-Based Computers,
;              4th Ed. by Kip R. Irvine
;------------------------------------------------------------
INCLUDE Irvine32.inc

.data
val1 SDWORD 8
val2 SDWORD -15
val3 SDWORD 20
source   byte "This is the source string",0
target   byte  SIZEOF source DUP(0)
nameSize = ($ - source) - 1
msgIntro byte "This is My Name's second assembly program and I will",0dh,0ah
         byte "demonstrate how to dump the registers using questions 1",0dh,0ah
         byte "on page 96, question 1 on page 133, and question 7 and 8",0dh,0ah
         byte "on page 134.",0dh,0ah
         byte 0dh,0ah,0
msg1     byte "------------------------------------",0dh,0ah
         byte "Demonstrating question 1 on page 96.",0dh,0ah
         byte "------------------------------------",0dh,0ah,0
msg2     byte "-------------------------------------",0dh,0ah
         byte "Demonstrating question 1 on page 133.",0dh,0ah
         byte "-------------------------------------",0dh,0ah,0
msgAdd   byte "Adding 0FFh and 1h in register al to set carry flag",0dh,0ah,0
msgSub   byte "Subtracting 3h from 2h in register ax to set carry flag",0dh,0ah,0
msg3     byte "-------------------------------------",0dh,0ah
         byte "Demonstrating question 7 on page 134.",0dh,0ah
         byte "-------------------------------------",0dh,0ah,0
msgExpr  byte "Solving expression EAX = -val2 + 7 - val3 + val1",0dh,0ah,0
msg4     byte "-------------------------------------",0dh,0ah
         byte "Demonstrating question 8 on page 134.",0dh,0ah
         byte "-------------------------------------",0dh,0ah,0
normOrd  byte "Normal order:",0dh,0ah,0
revOrd   byte "Reverse order:",0dh,0ah,0

.code
main PROC
;///////Intro Message/////////////////////////////////
	mov edx,OFFSET msgIntro ;intro message into edx
        call WriteString        ;display msgIntro

;///////Question 1 on page 96/////////////////////////
        mov edx,OFFSET msg1     ;message 1 into edx
        call WriteString        ;display msg1
	mov eax, 1000h		;1000h
	mov ebx, 4000h		;4000h
	mov ecx, 2000h		;2000h
	sub ebx, eax            ;4000h-1000h = 3000h
	sub ebx, ecx            ;3000h-2000h = 1000h
	call DumpRegs		;display the registers
	call WaitMsg            ;use call WaitMsg from page 146 to pause

;///////Question 1 on page 133/////////////////////////
        mov edx,OFFSET msg2     ;message 2 into edx
        call WriteString        ;display msg2

        ;Add to get carry flag
        mov edx,OFFSET msgAdd   ;message Add into edx
        call WriteString        ;display msgAdd
        mov al,0FFh
        add al,1h               ;al=100h CF=1 Because result of add is 100 so only 00
                                ;can go in the al register so the 1 is carried over
        call DumpRegs           ;display registers

        ;//Subtract to get carry flag
        mov edx,OFFSET msgSub   ;message Sub into edx
        call WriteString        ;display msgSub
        mov ax,2h
        sub ax,3h                ;ax=FFFFh CF=1 Because larger int - from smaller int
        call DumpRegs           ;display the registers
        call WaitMsg            ;use call WaitMsg from page 146 to pause

;///////Question 7 on page 134/////////////////////////
	mov edx,OFFSET msg3     ;message 3 into edx
	call WriteString        ;display msg3
	mov edx,OFFSET msgExpr  ;message Expr into edx
        call WriteString        ;display msgExpr
        mov eax,val2            ;mov val2 FFFFFFF1h in eax
        neg eax                 ;change sign of val2 in eax 0Fh
        add eax,7               ;add seven to eax 016h
        sub eax,val3            ;subtract val3 from eax 02h
        add eax,val1            ;add val1 to eax 0Ah
        call DumpRegs           ;display the registers
        call WaitMsg            ;use call WaitMsg from page 146 to pause

;///////Question 8 on page 134/////////////////////////
	mov edx,OFFSET msg4     ;message 4 into edx
	call WriteString        ;display msg4

	;//Using part of (CopyStr.asm) Pg.130-131 for reference
	mov esi,0               ;index register
        mov ecx,SIZEOF source   ;loop counter

	L1: ;//Loop
        mov al,source[esi]      ;get a character from source
        mov target[esi],al      ;store it in the target
        inc esi                 ;move to next character
        loop L1                 ;repeat for entire string

        ;//Print in normal order
        mov edx,OFFSET normOrd  ;message normOrd into edx
	call WriteString        ;display normOrd
	mov esi,OFFSET target   ;offset of variable
	mov ebx,1               ;byte format
	mov ecx,SIZEOF target-1 ;counter
	call DumpMem            ;display the block of memory in hex
        call WaitMsg            ;use call WaitMsg from page 146 to pause

        ;//Using part of (RevString.asm) Pg.157-158 for reference
        ;//Print in reverse order
        mov edx,OFFSET revOrd   ;message revOrd into edx
	call WriteString        ;display revOrd
        mov ecx,nameSize        ;push source on the stack
        mov esi,0

        L2: ;//Loop
        movzx eax,source[esi]   ;get character from source[esi] put in eax
	push eax                ;push on stack
	inc esi
	loop L2

        ;//Pop name in reverse and store in array
	mov ecx,nameSize
	mov esi,0

	L3: ;//Loop
	pop eax                 ;get characters form eax
	mov source[esi],al      ;store in string
	inc esi
	loop L3

	mov esi,OFFSET source   ;offset of variable
	mov ebx,1               ;byte format
	mov ecx,SIZEOF source-1 ;counter
	call DumpMem            ;display the block of memory in hex

	exit
main ENDP
END main

Dani AI

Generated

Nice idea, . When you say "dump the registers," the key is to first snapshot the CPU state and only then print it, so the act of printing does not destroy what you are trying to show. On x86 that means saving flags and GPRs, passing a pointer to that saved block to a printer, restoring the CPU state, and returning.

Here is a compact 32-bit NASM pattern (Linux or any environment where your print_regs32 routine can write to stdout). It saves EFLAGS and all GPRs, hands a pointer to the saved area to the printer, then restores everything.

; dump_regs32: snapshot -> print(saved_ptr) -> restore
dump_regs32:
    pushfd              ; save EFLAGS
    pushad              ; save EAX,ECX,EDX,EBX,ESP,EBP,ESI,EDI (in that order)
    mov    esi, esp     ; ESI points to saved block (top = EDI)

    ; layout after pushfd + pushad:
    ; [esi+00]=EDI [04]=ESI [08]=EBP [12]=ESP(before pushad)
    ; [16]=EBX  [20]=EDX [24]=ECX [28]=EAX [32]=EFLAGS

    push   esi
    call   print_regs32 ; print using the saved values, not the live regs
    add    esp, 4

    popad               ; restore GPRs
    popfd               ; restore flags
    ret

Notes and gotchas:

  • In 16-bit code use pushf/pusha (order: AX,CX,DX,BX,SP,BP,SI,DI). If you also need segments, you can push ds/es/ss/fs/gs as needed before the pusha.
  • In 64-bit there is no pushad; push each register you care about (pushfq, then rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi, r8–r15), pass the pointer, then pop in reverse.
  • Calling C printf will clobber caller-saved regs; that is fine because you print from the saved block, then restore with popad/popfd.
  • If you decode flags, do it from the saved EFLAGS/ RFLAGS word, not the live eflags.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.