flat assembler
Message board for the users of flat assembler.

Index > Linux > display register

Author
Thread Post new topic Reply to topic
ed_crumbpacker



Joined: 30 Jan 2015
Posts: 5
ed_crumbpacker 30 Jan 2015, 18:06
I give up. Could someone show me how to display the contents of a register (lets say eax) for debugging purposes.

If your wondering why I don't write this code myself, it is because the program I am trying to debug is "display the contents of register eax"...so if i can't see whats in it, I can't fix it...get the catch 22?

thanks
Post 30 Jan 2015, 18:06
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 30 Jan 2015, 20:40
You need to convert the register to string and then to write the string to the console using sys_write system call (#4), with file descriptor = 1 (STDOUT) or 2 (STDERR).
Post 30 Jan 2015, 20:40
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
ed_crumbpacker



Joined: 30 Jan 2015
Posts: 5
ed_crumbpacker 30 Jan 2015, 22:02
that's exactly what I'm trying to do. I have puppy linux so fasm is the only compiler (no c stuff). Every example I find uses printf or some other stuff. I tried 5 to 6 different methods but they all blow up or put out nothing and without a debugger I can't tell whats happening.
Post 30 Jan 2015, 22:02
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 30 Jan 2015, 22:55
Code:
    ;Prints RAX to STDOUT
    display_reg:
        lea rsi, [str_reg + 16]
        .loop:
            mov cl, al
            shr rax, 4
            and cl, 0x0F
            dec rsi
            add cl, '0'
            cmp cl, '9'
            jbe @f
                add cl, 'a' - '9' - 1
            @@:
            mov [rsi], cl
            test rax, rax
            jnz .loop
        mov edx, 17
        lea rsi, [str_reg]
        mov edi, STDOUT
        mov eax, sys_write
        syscall
        ret
    
Post 30 Jan 2015, 22:55
View user's profile Send private message Visit poster's website Reply with quote
ed_crumbpacker



Joined: 30 Jan 2015
Posts: 5
ed_crumbpacker 30 Jan 2015, 23:30
OK I had to change r to e because I'm 32-bit and created a str_reg as db ? (not sure if thats right). Is mov edi,STDOUT correct? Anyway I get a segmentation fault

Code:
format ELF executable
entry display_reg

str_reg   db   ?

;Prints RAX to STDOUT
display_reg:

     mov   eax,0x423f

  
        lea esi, [str_reg + 16]
        .loop:
                mov cl, al
                shr eax, 4
                and cl, 0x0F
                dec esi
                add cl, '0'
                cmp cl, '9'
                jbe @f
                        add cl, 'a' - '9' - 1
                @@:
                mov [esi], cl
                test eax, eax
                jnz .loop
        mov edx, 17
        lea esi, [str_reg]
        mov ebx, 1
        mov eax, 4
        int 0x80
        
_eoj:                         ;exit to LINUX
   mov    eax,1
   xor    ebx,ebx
   int    0x80    


another question...how do I get my code into one of those cool white boxes?

Edit by revolution: You now have a "cool white box"
Post 30 Jan 2015, 23:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20623
Location: In your JS exploiting you and your system
revolution 30 Jan 2015, 23:36
You need to make your buffer area larger to store the string:
Code:
str_reg   rb 32 ;make space for the entire string    
Post 30 Jan 2015, 23:36
View user's profile Send private message Visit poster's website Reply with quote
ed_crumbpacker



Joined: 30 Jan 2015
Posts: 5
ed_crumbpacker 31 Jan 2015, 05:10
thanks for the help...so this is the final outcome. When I run it all the register come out with about to same numbers

eax 8048054 ebx 8048058 ecx 804805C edx 8048060

This seems suspicious to me...I'll keep playing with it

Code:
format ELF executable
entry _start

reg_eax   rd   1
reg_ebx   rd   1
reg_ecx   rd   1
reg_edx   rd   1

prt_str   db   'e'
reg_id    db   ' '
          db   'x:'
str_reg   rb   32
str_end   db   0xa
str_reg_size = $-prt_str


_start:
   mov    [reg_eax],eax       ;save off registers
   mov    [reg_ebx],ebx
   mov    [reg_ecx],ecx
   mov    [reg_edx],edx

   mov    eax,reg_eax
   mov    [reg_id],'a'        ;place the reg letter in label
   call   display_reg
   
   mov    eax,reg_ebx
   mov    [reg_id],'b' 
   call   display_reg

   mov    eax,reg_ecx
   mov    [reg_id],'c' 
   call   display_reg
   
   mov    eax,reg_edx
   mov    [reg_id],'d' 
   call   display_reg
   jmp    _eoj

display_reg:
   lea    esi, [str_reg + 16]   
.loop:
   mov    cl, al                ;mov byte 
   shr    eax, 4                ;remove small byte
   and    cl, 0x0F              ;
   dec    esi                   ;
   add    cl, '0'               ;ex. if cl = 2 + 48 = 50 (ascii '2')
   cmp    cl, '9'
   jbe    @f
   add    cl, 'a' - '9' - 1     ;ex. 97 - 57 - 1 = 39 + 58 = 97(a)
@@:
   mov    [esi], cl             ;move character to esi
   test   eax, eax
   jnz    .loop

   mov    eax, 4                ;print the register
   mov    ebx, 1
   mov    ecx, prt_str
   mov    edx, str_reg_size
   int    0x80
   ret
        
_eoj:                           ;exit to LINUX
   mov    eax,1
   xor    ebx,ebx
   int    0x80    
Post 31 Jan 2015, 05:10
View user's profile Send private message Reply with quote
RIxRIpt



Joined: 18 Apr 2013
Posts: 50
RIxRIpt 31 Jan 2015, 11:38
Quote:

When I run it all the register come out with about to same numbers

Because you are loading address of register value into eax: `mov eax,reg_ebx` instead of loading register value into eax: `mov eax,[reg_ebx]`
And 8 bytes for str_reg must be enough (instead of 32):
Code:
str_reg 8 dup '0' ;duplicate '0' eight times    

And please note that old contents of str_reg are not cleared out:
Code:
test   eax, eax ;exists loop as soon as eax is is empty
jnz    .loop    

If you don't want a counter, here're some ugly(?) fixes:
Code:
;Forcing to loop thru all digits of register:
cmp esi, str_reg ;instead of `test eax, eax`

;Or clearing out str_reg before the loop:
mov [str_reg + 0], '0000'
mov [str_reg + 4], '0000'
    
Post 31 Jan 2015, 11:38
View user's profile Send private message Visit poster's website Reply with quote
ed_crumbpacker



Joined: 30 Jan 2015
Posts: 5
ed_crumbpacker 31 Jan 2015, 15:20
yeah...I saw that this morning...shouldn't write code and watch hockey at the same time
Post 31 Jan 2015, 15:20
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.