flat assembler
Message board for the users of flat assembler.

Index > Linux > [Solved] Need help in creating a hexdump utility

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 126
FlierMate
I have done something like this, generating using HLL:

Code:
00000000    7F  45  4C  46  02  01  01  03  00  00  00  00  00  00  00  00      .ELF............
00000010    02  00  3E  00  01  00  00  00  B0  00  40  00  00  00  00  00      ..>.......@.....
00000020    40  00  00  00  00  00  00  00  00  00  00  00  00  00  00  00      @...............
00000030    00  00  00  00  40  00  38  00  02  00  40  00  00  00  00  00      ....@.8...@.....    


Now I want to create the hexadecimal dump output using Assembly language:

I managed to reach the stage where I can open file, read file, and write the output.
Still figuring out how to convert ASCII character code to printable hexadecimal value....

I paste the code on here as my backup:

Code:
format ELF64 executable 3

segment readable executable

entry $

      pop     r8
      pop     rsi       ;APP_NAME
      pop     rsi       ;1st command-line argument
      mov     rdi,rsi
      cmp     rdi,0
      je      _err   
      ;lea     rdi,[fn]
      xor     rsi,rsi   ;O_RDONLY
      mov     rax,2     ; sys_create
      syscall
      cmp     rax,-1
      je      _err
      mov     dword [fd],eax

_redo:
      mov     rdx,16
      lea     rsi,[buffer]      
      mov     edi,dword [fd]
      xor     rax,rax  ; sys_read
      syscall
      cmp     rax,0
      je      _err

      mov     rdx,rax
      lea     rsi,[buffer]
      mov     rdi,1    ; STDOUT
      mov     rax,1    ; sys_write
      syscall
      jmp     _redo
      
      mov     edi,dword [fd]
      mov     rax,3    ; sys_close
      syscall

_err:
      mov     rdi,rax
      mov     rax,60   ; sys_exit
      syscall

segment readable writeable

buffer rb      16
fd     dd      ?
;fn     db      'cpubrand.asm',0    


Last edited by FlierMate on 01 Oct 2021, 16:38; edited 1 time in total
Post 01 Oct 2021, 09:56
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7937
Location: Kraków, Poland
Tomasz Grysztar
FlierMate wrote:
Still figuring out how to convert ASCII character code to printable hexadecimal value....
In one of the parts of my video tutorial I show an implementation of a "ShowHex" routine in detail. Even though it is shown under Windows, these instructions and snippets are not really OS-dependent.
Post 01 Oct 2021, 10:22
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 126
FlierMate
Tomasz Grysztar wrote:
FlierMate wrote:
Still figuring out how to convert ASCII character code to printable hexadecimal value....
In one of the parts of my video tutorial I show an implementation of a "ShowHex" routine in detail. Even though it is shown under Windows, these instructions and snippets are not really OS-dependent.


Oh wow, I did not know you have YouTube channel too.

I used your code as below:

Code:
_redo:
...
...
      mov     edx,[offset]
      mov     ecx,8
      call    ConvertHex
      call    PrintOffset
      call    PrintLongSpace   
...
...
...
      call    PrintLine      
      add     [offset],16
      jmp     _redo
...
...
...

ConvertHex:                                     Wink Nice code snippet by Tomasz Grysztar (flat assembler)
      ;mov      ecx,8
      xor      ebx,ebx
_loop1:
      rol      edx,4
      mov      eax,edx
      and      eax,1111b
      mov      al,[digits+eax]
      mov      [ebx+hexnum],al
      inc      ebx
      dec      ecx
      jnz      _loop1     
      ret     


It works by showing the offset (left-most pane in screenshot), however, it did not show the data in proper hex values (all are 0x00).

I am a little confused here, "FFh" is correctly converted, but how about 0xFF (value)?

The code to convert data is highlighted below:

Code:
      mov     rcx,0
      
_repeat1:      
      xor     rdx,rdx
      mov     dl,byte [buffer + rcx]

      push    rcx
      mov     rcx,2
      call    ConvertHex
....
....
    


Description: Convert hex offset successfully but data (hex) are all 0x00
Filesize: 46.99 KB
Viewed: 292 Time(s)

Screenshot from 2021-10-01 21-06-52.png


Post 01 Oct 2021, 13:12
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 126
FlierMate
Ahh, I found out I should have customize the code given by Tomasz.

For 2-byte hex digit use case:

Code:
....
....
      rol      dl,4
      mov      al,dl
      and      eax,1111b
      mov      al,[digits+eax]
....
....    


This fixes the error. Cheers!


Description: Hexdump now works correctly! Hurray
Filesize: 58.34 KB
Viewed: 271 Time(s)

Screenshot from 2021-10-02 00-32-17.png


Post 01 Oct 2021, 16:34
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 126
FlierMate
Somewhat off topic: To my surprise, my new hexdump file utility is currently the ONLY repo written in Assembly language under "hexdump" topic / tag.

Ville wrote:
Currently Menuet is the only OS written 100% in assembly with modern feature-set and it took us some time to realize this goal. This is also an easily identifiable place in the computing world.


Although hexdump is far simpler project, but it makes me feel like that way too...... Embarassed


Description: "hexdump" topic on GitHub
Filesize: 126.02 KB
Viewed: 185 Time(s)

hexdump.png


Post 11 Oct 2021, 20:48
View user's profile Send private message Visit poster's website Reply with quote
st



Joined: 12 Jul 2019
Posts: 49
Location: Russia
st
What is the difference between topic/tag and search (which gives 23 repository results)?
Post 16 Oct 2021, 13:20
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 126
FlierMate
st wrote:
What is the difference between topic/tag and search (which gives 23 repository results)?


Topic/tag is labelled voluntarily by the repo owner. Your search result is otherwise, which gives more accurate matches.

However, repos could also be using keyword other than "hexdump", which in turn hard to tell how many hexdump utility there are on GitHub.

I see most if not all code repositories of hexdump written in Assembly are not popular, most are not starred, and my "hexdump" FASM example has had only 3 git clones so far.
Post 16 Oct 2021, 13:45
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.