flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > DOS > HEX -- encode hex from cmdline into data file

Author
Thread Post new topic Reply to topic
rugxulo



Joined: 09 Aug 2005
Posts: 2122
Location: Usono (aka, USA)
HEX -- encode hex from cmdline into data file
Another monstrosity! Shocked I'm almost ashamed to post it Embarassed , but whatever! Laughing Cool

So what do you do if you don't have FASM (or Debug or whatnot) and want to encode raw hex values into a data file? I guess you could Alt-num (decimal) it piecemeal, but this way seemed slightly better.

Bug reports and fixes welcome!! (As if anybody cares anymore, sigh. Rolling Eyes )


Code:

; HEX.ASM -- encode hex from cmdline into data file
;
; rugxulo _AT_ gmail
;
; public domain, free for any use, nenies proprajho


;NECSUX=1

ARGV=81h
BEEP=7
CR=13
LF=10

CLOSE=3Eh
CREATE=5Bh
GOODBYE=4Ch
OPENRW=3D02h
SEEKEND=4202h
WRITE=40h
WRITESTR=9

DOS equ int 21h

b equ byte
w equ word

macro AAD16 {
if defined NECSUX
 aad 16 ; ?? maybe unsupported by NEC V20/V30 ??
else
 rept 4 \{ shl ah,1 \}
 add al,ah
end if
}

macro AAM16 {
if defined NECSUX
  aam 16 ; ?? maybe unsupported by NEC V20/V30 ??
else
  mov ah,al
  and al,15
  rept 4 \{ shr ah,1 \}
end if
}

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

; section .text

org 100h ; DOS .COM

Main:
  mov di,ARGV
  cmp b[di],CR
  jz Fino
  xor ch,ch
  mov cl,b[di-1]
  inc cl
  mov al,' '
  rep scasb
  dec di
  cmp b[di],CR
  jz Fino
.load:
  mov si,di
  mov di,buf
.read:
  lodsw
  call convert
  xchg ah,al
  call convert
  AAD16
  stosb
  inc w[count]
.check:
  cmp b[si],CR
  jnz .read

; int3
  call create
Fino:
  mov ah,GOODBYE
  mov al,b[errlvl]
  DOS
; end Main


; proc
create:
  mov ah,CREATE
  xor cx,cx
  mov dx,filename
  DOS
  jnc .handled
.append:
  mov ax,OPENRW
  mov dx,filename
  DOS
  jc .ret
  mov bx,SEEKEND
  xchg ax,bx
  xor cx,cx
  xor dx,dx
  push bx
  DOS
  pop ax
  jc .ret
.handled:
  mov w[handle],ax
  xchg ax,bx
.write:
  mov ah,WRITE
  mov cx,w[count]
  mov dx,buf
  DOS
  jnc .howmany
  xor al,al
.howmany:
  mov b[errlvl],al
.close:
  mov ah,CLOSE
; mov bx,w[handle]
  DOS
  jnc .ret
  mov b[errlvl],0
.ret:
  ret
; endp


; proc
convert:
  cmp al,'0'
  jb badhex
  cmp al,'F'
  ja badhex
  cmp al,'9'
  jbe .fix
  cmp al,'A'
  jb badhex
  sub al,'A'-'9'-1 ; 7
.fix:
  sub al,'0'
  ret
; endp


badhex:
; int3
  pop dx ; adjust the stack (why bother?)
; nop
  cmp al,'~'
  ja .nonprintable
  cmp al,' '
  jb .nonprintable
  mov b[errmsg+3],al
.warn:
  mov ah,WRITESTR
  mov dx,errmsg
  DOS
  mov b[errlvl],255
  jmp Fino
.nonprintable:
  mov di,errmsg+2
  AAM16
  xchg ah,al
  NIB2ASC
  stosb
  mov al,ah
  NIB2ASC
  mov ah,'h'
  stosw
  jmp .warn


; section .data

filename db 'hex.dat',0
errmsg db CR,LF,"'?' is invalid!",CR,LF,'$'
errlvl db 0
count dw 0

; section .bss

handle rw 1
buf rb 128 ; or probably only 64? (dunno)

; EOF


Post 09 Sep 2016, 02:48
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.