Author Topic: How to see register value in FreeBSD Nasm?  (Read 9510 times)

nobody

  • Guest
How to see register value in FreeBSD Nasm?
« on: June 25, 2009, 03:32:56 PM »
I have read the guidline from this webpage,

and try the "Hello.asm" file to print hello,world

in my FreeBSD workstation.

But i got some problem when i try to program more example of Nasm

That is i can't debug with my program.


When i used MASM with Irine libery, I chould use "dumpreg"

to show the value of eax,ebx...etc


But i can't find some function of Nasm like that.

Can anyone give me some hint or website to learn?

THX!!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to see register value in FreeBSD Nasm?
« Reply #1 on: June 27, 2009, 02:23:45 PM »
I'm not familiar with the Irine library. I assume they've got code to do it, possibly wrapped in a macro.

Dr. Carter's got such a setup in the example code from http://drpaulcarter.com/pcasm

Uses printf. I like to "do it myself". Here's a rough first draft. It's for Linux, you'll have to fix the "exit" and the "write_stdout" for BSD (holler if you need help with that - I can take a guess).

It can be improved... Probably should show segment registers, too. Display of the flags could be expanded. What do you *want* it to do?

Best,
Frank

; nasm -f elf dumpregs.asm
; ld -o dumpregs dumpregs.o

global _start

section .text
_start:

mov eax, 1
    mov ebx, 2
    mov ecx, 3
    mov edx, 4
    mov esi, 5
    mov edi, 6
    mov ebp, 7
    call dumpregs

mov eax, 1
    int 80h

;-------------------
dumpregs:
    pusha
    pushf
    mov ebp, esp
    sub esp, 1024 ; ?

mov ecx, esp
    xor edx, edx
    mov dword [ecx + edx], 'eax='
    add edx, byte 4
    mov eax, [ebp + 32]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'ebx='
    add edx, byte 4
    mov eax, [ebp + 20]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'ecx='
    add edx, byte 4
    mov eax, [ebp + 28]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'edx='
    add edx, byte 4
    mov eax, [ebp + 24]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'esi='
    add edx, byte 4
    mov eax, [ebp + 8]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'edi='
    add edx, byte 4
    mov eax, [ebp + 4]
    call dumpregs2
    mov byte [ecx + edx], 10
    inc edx

mov dword [ecx + edx], 'ebp='
    add edx, byte 4
    mov eax, [ebp + 12]
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'esp='
    add edx, byte 4
    mov eax, [ebp + 16]
    add eax, byte 4
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'eip='
    add edx, byte 4
    mov eax, [ebp + 36]
    sub eax, byte 5
    call dumpregs2
    mov byte [ecx + edx], ' '
    inc edx

mov dword [ecx + edx], 'flg='
    add edx, byte 4
    mov eax, [ebp + 0]
    call dumpregs2
    mov byte [ecx + edx], 10
    inc edx


    call write_stdout
    mov esp, ebp
    popf
    popa
    ret

dumpregs2:

mov ebx, 8
.top:
    rol eax, 4
    push eax
    and al, 0Fh
    cmp al, 10
    sbb al, 69h
    das
    mov [ecx + edx], al
    inc edx
    pop eax
    dec ebx
    jnz .top
    ret
;---------------

write_stdout:
    mov eax, 4
    mov ebx, 1
    int 80h
    ret

mene mene tekel

  • Guest
Re: How to see register value in FreeBSD Nasm?
« Reply #2 on: June 27, 2009, 02:47:11 PM »
Hmm, BSD no glue... but if it helps I could provide my simple MemReg.dll  (win32) exposing methods to print following stuff to the console:

----- C P U - F L A G S -------------------------------------------------------
EFL     : 0x00000246 = 0000 0000 0000 0000 0000 0010 0100 0110 (binary)
 Status : CF=0 (00), PF=1 (02), AF=0 (04), ZF=1 (06), SF=0 (07), OF=0 (11)
 Control: DF=0 (10)
 System : TP=0 (08), IF=1 (09), IO=00 (12-13,binary), NT=0 (14)
          RF=0 (16), VM=0 (17), AC=0 (18), VF=0 (19), VP=0 (20), ID=0 (21)
----- C P U - R E G I S T E R -------------------------------------------------
EAX 0x75FAD0D7 =  1979371735 = 0111 0101 1111 1010 1101 0000 1101 0111 (binary)
EBX 0x7FFDA000 =  2147328000 = 0111 1111 1111 1101 1010 0000 0000 0000 (binary)
ECX 0x00000000 =           0 = 0000 0000 0000 0000 0000 0000 0000 0000 (binary)
EDX 0x00403000 =     4206592 = 0000 0000 0100 0000 0011 0000 0000 0000 (binary)
ESI 0x00000000 =           0 = 0000 0000 0000 0000 0000 0000 0000 0000 (binary)
EDI 0x00000000 =           0 = 0000 0000 0000 0000 0000 0000 0000 0000 (binary)
EBP 0x0012FF94 =     1245076 = 0000 0000 0001 0010 1111 1111 1001 0100 (binary)
ESP 0x0012FF8C =     1245068 = 0000 0000 0001 0010 1111 1111 1000 1100 (binary)
EIP 0x00403000 (entry), 0x00403016 (exit)
ES=0x0023, CS=0x001B, SS=0x0023, DS=0x0023, FS=0x003B, GS=0x0000
----- F P U - F L A G S -------------------------------------------------------
FSW  (Status): 0x0120 = 0000 0001 0010 0000 (binary)
 Conditions  : C0=1 (08), C1=0 (09), C2=0 (10), C3=0 (14)
 Top Stack   : TS=0 (11-13)
 Errors      : SF=0 (06), ES=0 (07), FB=0 (15)
 Exceptions  : IE=0 (00), DE=0 (01), ZE=0 (02), OE=0 (03), UE=0 (04), PE=1 (05)
FCW (Control): 0x027F = 0000 0010 0111 1111 (binary)
 Controller  : PC=10 (08-09,binary), RC=00 (10-11,binary), IC=0 (12)
 Except.Mask : IM=1 (00), DM=1 (01), ZM=1 (02), OM=1 (03), UM=1 (04), PM=1 (05)
FTW     (Tag): 0xFFFF = 11 11 11 11 11 11 11 11 (binary)
               matches  T7 T6 T5 T4 T3 T2 T1 T0
----- F P U - R E G I S T E R -------------------------------------------------
ST0 empty 0000 00000000 00000000 ( 0.0000000000000000e+000)
ST1 empty 0000 00000000 00000000 ( 0.0000000000000000e+000)
ST2 empty 401D CF051FA8 00000000 ( 1.7366097480000000e+009)
ST3 empty 403D CF064B68 00000000 ( 7.4588468628800143e+018)
ST4 empty 0000 00000000 00000000 ( 0.0000000000000000e+000)
ST5 empty 403C CCEC70E0 C08498B0 ( 3.6915753472114785e+018)
ST6 empty 4039 C584E604 00842360 ( 4.4477355085189146e+017)
ST7 empty 403C ECE8F084 D0E09CE8 ( 4.2677897101946491e+018)
----- M E M O R Y -------------------------------------------------------------
Address   *B *C *D *E  *F *0 *1 *2 - *3 *4 *5 *6  *7 *8 *9 *A  BCDEF0123456789A
--------  -- -- -- --  -- -- -- --   -- -- -- --  -- -- -- --  ----------------
0040109B  00 01 02 03  04 05 06 07 - 08 09 0A 0B  0C 0D 0E 0F  ................
004010AB  10 11 12 13  14 15 16 17 - 18 19 1A 1B  1C 1D 1E 1F  ????¶§??????????
004010BB  20 21 22 23  24 25 26 27 - 28 29 2A 2B  2C 2D 2E 2F   !"#$%&'()*+,-./
004010CB  30 31 32 33  34 35 36 37 - 38 39 3A 3B  3C 3D 3E 3F  0123456789:;<=>?
004010DB  40 41 42 43  44 45 46 47 - 48 49 4A 4B  4C 4D 4E 4F  @ABCDEFGHIJKLMNO
004010EB  50 51 52 53  54 55 56 57 - 58 59 5A 5B  5C 5D 5E 5F  PQRSTUVWXYZ[\]^_
004010FB  60 61 62 63  64 65 66 67 - 68 69 6A 6B  6C 6D 6E 6F  `abcdefghijklmno
0040110B  70 71 72 73  74 75 76 77 - 78 79 7A 7B  7C 7D 7E 7F  pqrstuvwxyz{|}~?
0040111B  80 81 82 83  84 85 86 87 - 88 89 8A 8B  8C 8D 8E 8F  ÇüéâäàåçêëèïîìÄ

Nathan

  • Guest
Re: How to see register value in FreeBSD Nasm?
« Reply #3 on: June 29, 2009, 04:05:56 AM »
tekel -
If you don't mind, I would like to see the source of you MemReg.dll -- it would come in handy for a lot of situations.

Nathan.