flat assembler
Message board for the users of flat assembler.

Index > DOS > DB -- dump bytes (hexdump)

Author
Thread Post new topic Reply to topic
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 02 Mar 2016, 04:11
Okay, so here's some "craptastic" Shocked code that I whipped up recently.

I know it's not great, and I should probably rewrite it. (I've already written it in a few HLLs too, for comparison. It's not terribly complex.)

Anyways, if anyone wants to improve it (or give real-world tips) or especially benchmark it across various cpus, that'd be great! Cool

In case you can't tell, I was very naive, so a lot of attempts to optimize didn't really help at all. I don't pretend this is worthy of anyone's time, just mildly curious. Embarassed

Code:
; ----------------------------------------------------------------------
; DB.ASM -- dump raw bytes as hex from a file (using FASM)
;
; public domain, free for any use, nenies proprajho
;
; rugxulo _AT_ gmail
;
;
; Thursday, December 24, 2015
; ----------------------------------------------------------------------

;USE_186=1
;USE_386=1
;USE_AAM=1
;USE_ALIGN=1
;USE_XLATB=1

ARGC=80h
CLOSE=3Eh
CR=13
DOS=21h
GOODBYE=20h
LF=10
LFCR=0A0Dh
LINEMAX=16
OPENREAD=3D00h
READBYTES=3Fh
WRITESTR=9

macro ALIGNED {
if defined USE_ALIGN
  align 16
end if
}

org 100h ; DOS .COM

; section .text

; begin main
Komenco:
  mov si,ARGC
  lodsb
  xor bx,bx
  xchg al,bl
  mov byte [si+bx],bh
.open:
  cmp byte [si],CR
  jz Fino
  lea dx,[si+1]
  mov ax,OPENREAD
  int DOS
  jc Fino
  mov word [handle],ax
.read:
  mov ah,READBYTES
  mov bx,word [handle]
  mov cx,LINEMAX
  mov dx,buf
  int DOS
  jc .close
  mov word [bytesread],ax
  test ax,ax
  jz .close
.show:
  call writeline
if defined USE_386
  movzx eax,word [bytesread]
  add dword [ofs],eax
else
  mov ax,word [bytesread]
  add word [ofs],ax
  adc word [ofs+2],0
end if
  jmp .read
.close:
  call setupofs
  mov di,line+8
  call writeline.writestr
  mov ah,CLOSE
  mov bx,word [handle]
  int DOS
Fino:
  int GOODBYE
; end main


ALIGNED
; proc
setupofs:
  mov bx,6
  mov cx,4
  mov si,ofs
.ofsbyte:
  lea di,[line+bx]
  lodsb
  call split
  stosw
  sub bx,2
  loop .ofsbyte
  ret


ALIGNED
; proc
writeline:
  call setupofs
.setupbytes:
  mov cx,word [bytesread]
  mov si,buf
  mov di,line.bytes
.load:
  lodsb
  call split
  stosw
  inc di
  loop .load
  dec di
.writestr:
  mov ax,LFCR
  stosw
  mov byte [di],'$'
  mov ah,WRITESTR
  mov dx,line
  int DOS
  ret
; endp


ALIGNED
; proc
split:
  push bx
if defined USE_AAM
display "AAM 16"
  aam 16 ; unsupported by NEC V20/V30
else
  xor ah,ah
  if defined USE_186
    shl ax,4
    shr al,4
  else
    shl ax,1
    shl ax,1
    shl ax,1
    shl ax,1
    shr al,1
    shr al,1
    shr al,1
    shr al,1
  end if
end if
.byte2hexasc:
if defined USE_XLATB
  mov bx,hexarray
  xlatb
  xchg ah,al
  xlatb
else
  cmp al,10
  sbb al,105
  das
  mov bl,al
  mov al,ah
  cmp al,10
  sbb al,105
  das
  mov ah,bl
end if
.bye:
  pop bx
  ret
; endp


; section .data

ALIGNED

line   db 'HHHHHHHH: '
.bytes db 'XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'
.end   db CR,LF,'$'

if defined USE_XLATB
hexarray db '0123456789ABCDEF'
end if

ofs dd 0

; section .bss

bytesread rw 1
handle rw 1
buf rb LINEMAX
    
Post 02 Mar 2016, 04:11
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 28 Aug 2016, 02:42
Well, not that anybody else cares, but here's a slightly newer version I've been fiddling with. No major speedups, though. Further studying (and testing) is required.

Code:
; ----------------------------------------------------------------------
; DB.ASM -- dump raw bytes as hex from a file (using FASM)
;
; public domain, free for any use, nenies proprajho
;
; rugxulo _AT_ gmail
;
;
; Saturday, August 20, 2016
; ----------------------------------------------------------------------

;USE_186=1
;USE_386=1
;USE_AAM=1
;USE_ALIGN=1
;USE_BLOAT=1
;USE_MMX=1
;USE_XLATB=1

ARGC=80h
CLOSE=3Eh
CR=13
DOS=21h
GOODBYE=20h
LF=10
LFCR=0A0Dh
LINEMAX=16
OPENREAD=3D00h
READBYTES=3Fh
WRITESTR=9

macro ALIGNED {
if defined USE_ALIGN
if defined ALIGNMENT
  align ALIGNMENT
else
  align 16
end if
end if
}

macro NIB2ASC {
  cmp al,10
  sbb al,105
  das
}

org 100h ; DOS .COM

; section .text

; begin main
Komenco:
  mov si,ARGC
  lodsb
  xor bx,bx
  xchg al,bl
  mov byte [si+bx],bh
.open:
  cmp byte [si],CR
  jz Fino
  lea dx,[si+1]
  mov ax,OPENREAD
  int DOS
  jc Fino
  mov word [handle],ax
ALIGNED
.read:
  mov ah,READBYTES
  mov bx,word [handle]
  mov cx,LINEMAX
  mov dx,buf
  int DOS
  jc .close
  mov word [bytesread],ax
  test ax,ax
  jz .close
.show:
  call writeline
if defined USE_386
  movzx eax,word [bytesread]
  add dword [ofs],eax
else
  mov ax,word [bytesread]
  add word [ofs],ax
  adc word [ofs+2],0
end if
  jmp .read
.close:
  call setupofs
  mov di,line+8
  call writeline.writestr
  mov ah,CLOSE
  mov bx,word [handle]
  int DOS
Fino:
if defined USE_MMX
  emms
end if
  int GOODBYE
; end main


ALIGNED
; proc
setupofs:
if defined USE_MMX
  mov eax,[ofs]
  bswap eax
  movd mm0,eax
.convert:
  punpcklbw mm0,mm0
  movq mm1,mm0
  psrlw mm0,4
  pand mm0,qword [mask]
  pand mm1,qword [mask]
  psrlw mm0,8
  por mm0,mm1
  paddb mm0,[zeros]
  movq mm2,mm0
  pcmpgtb mm2,[nines]
  pand mm2,qword [sevens]
  paddb mm0,mm2
  movq qword [line],mm0
else
  mov cx,4
  mov si,ofs
  mov di,line+6
.ofsbyte:
  rept 4 {
    lodsb
    call split
    mov [di],ax
    sub di,2
  }
end if
  ret
; endp


ALIGNED
; proc
writeline:
  call setupofs
.setupbytes:
  mov cx,word [bytesread]
  mov si,buf
  mov di,line.bytes
ALIGNED
.load:
  lodsb
  call split
  mov [di],ax
  add di,3
  loop .load
  dec di
.writestr:
  mov word [di],LFCR
  mov byte [di+2],'$'
  mov ah,WRITESTR
  mov dx,line
  int DOS
  ret
; endp


ALIGNED
; proc
split:
if defined USE_AAM
display "AAM 16 is unsupported by NEC V20/V30"
  aam 16
else
  mov ah,al
  and al,15
  if defined USE_186
    shr ah,4
  else
    rept 4 { shr ah,1 }
  end if
end if
.byte2hexasc:
if defined USE_XLATB
  push bx
  mov bx,hexarray
  xlatb
  xchg ah,al
  xlatb
  pop bx
else
  NIB2ASC
  mov dl,al
  mov al,ah
  NIB2ASC
  mov ah,dl
end if
.bye:
  ret
; endp


; section .data

if defined USE_BLOAT
if defined BLOAT
  db BLOAT dup(0)
else
  db 4*1024 dup(0)
end if
end if

ALIGNED
  line   db 'HHHHHHHH: '
  .bytes db 'XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'
  .end   db CR,LF,'$'

if defined USE_MMX
ALIGNED
  zeros  dq '00000000'
  nines  dq '99999999'
  sevens db 8 dup(7)
  mask   dw 4 dup(0Fh shl 8)
end if

if defined USE_XLATB
ALIGNED
  hexarray db '0123456789ABCDEF'
end if

ALIGNED
  ofs dd 0

; section .bss

ALIGNED
  bytesread rw 1

ALIGNED
  handle rw 1

ALIGNED
  buf rb LINEMAX

; EOF
    
Post 28 Aug 2016, 02:42
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4071
Location: vpcmpistri
bitRAKE 28 Aug 2016, 19:05

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 28 Aug 2016, 19:05
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 04 Sep 2016, 01:07
bitRAKE wrote:
http://wm.ite.pl/articles/convert-to-hex.html


While I didn't study this in-depth and while it's still vaguely interesting, it doesn't show anything uniquely amazing. Maybe I'll have to time/test some of it myself, but at first glance nothing obvious pops out. (Besides, I already reluctantly wrote some crude MMX, and it didn't help at all. Alignment seems to make a bigger difference, sadly.)

Anyways, I also briefly looked at Hugi Compo 17 (2002), but even that didn't show any obvious hints. (The "obvious" hint now is that slow hard disk seeking is the primary bottleneck, hence I need a better buffer algorithm. Except for one or two weirdo compilers, the ANSI C equivalent is always lightning fast, thanks to buffered I/O.)

Anyways, I started to vainly wonder if I could shrink the code size. So I wrangled it down to 175 bytes. I also wanted a short (one-page) source version, so I converted it to Octasm. Not counting blanks, it's only 23 lines.

Then I wondered if I could do similar terseness with a FASM version. I almost just cobbled together my own tr utility just to be able to put multiple instructions on one line, but in the end I found I could use a macro to help out. So 39 lines (not counting blanks) isn't bad. (It also adds up to only 71 instructions.)

Yeah, pointless but still fun! Cool

Code:
org 100h ; 175 bytes

MAX=16

b equ byte
w equ word
DOS equ int 21h
AAM16 equ aam 16

macro do [instr] { forward instr }
;macro AAM16 { do <mov ah,al>,<and al,15>,<shr ah,4> }

Komenco:
 do <mov si,80h>,lodsb,<xor bx,bx>,<xchg al,bl>,<mov [si+bx],bh>
open:
 do <cmp b[si],13>,jz Fino,<lea dx,[si+1]>,<mov ax,3D00h>,DOS
 do jc Fino,<mov [handle],ax>
read:
 do <mov ah,3Fh>,<mov bx,[handle]>,<mov cx,MAX>,<mov dx,buf>,DOS
 do jc close,<mov [count],ax>,<test ax,ax>,jz close
show:
 do call writeln,<mov ax,[count]>,<add w[ofs],ax>,<adc w[ofs+2],0>,jmp read
close:
 do call setupofs,<mov di,line+8>,call writestr,<mov ah,3Eh>
 do <mov bx,[handle]>,DOS
Fino:
 int 20h

setupofs:
 do <mov si,ofs>,<mov di,line+6>,<mov w[di+2],': '>,<mov cx,4>
@@:
 do lodsb,call split,stosw,<sub di,4>,loop @b,ret

writeln:
 do call setupofs,<mov cx,[count]>,<mov si,buf>,<mov di,line+10>
@@:
 do lodsb,call split,stosw,<mov al,' '>,stosb,loop @b,dec di
writestr:
 do <mov w[di],0A0Dh>,<mov b[di+2],'$'>,<mov ah,9>,<mov dx,line>,DOS,ret

split:
 AAM16
 do <cmp al,10>,<sbb al,105>,das,<mov dl,al>,<mov al,ah>
 do <cmp al,10>,<sbb al,105>,das,<mov ah,dl>,ret

do ofs dd 0,count dw ?,handle dw ?,buf db MAX dup(?)
line db 10+(MAX*3)-1+3 dup(?)
    
Post 04 Sep 2016, 01:07
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.