Message board for the users of flat assembler.
> DOS > X2B -- hex string to binary string
So when working on my (extremely simple) HEX program, it was grabbing hex (supporting uppercase only) using a bunch of comparisons/jumps, which I think is correct but banal and inefficient. (Although I swear jumps aren't nearly as bad as people claim, but minimizing them isn't the worst idea either.)
I had an older (naive, pathetic) number base converter program that had some mini-routine (cryptic) hex conversion that I must've gleaned from someone else ("cmp al,40h", "adc al,69h", "daa"). It does work, but again, uppercase only (so I have to massage input first).
So, in that line of thinking, I built up this simple example to test it. I was trying to find a (slightly) "better" way ... although there is no (obvious to me, yet) "perfect" way.
The validate routine is because it's too annoying having to be precise to avoid invalid chars. (Even avoiding extra blanks is practically mandatory.) Plus, only accepting uppercase is confusing (esp. without errorchecking! any input translated to incorrect output, without warning, isn't helpful!). Building the table at runtime is because it felt braindead to supply 256 bytes of bloat just for this (esp. when the whole program is smaller than that).
The two versions of writebin weren't necessary, but I felt the old, naive way was too ad hoc and sloppy. (Simple testing doesn't show much speed difference, roughly only 2% improvement, so it's not worth worrying. Same with size, it matters little because it's all wasting 512 bytes minimum anyways. But I left it for completeness.)
And no, in the .ASM I didn't bother mashing multiple instructions together (like my shorter hexdump variant) because, although the lines here are criminally short, it's harder to understand the program otherwise.
; X2B.ASM -- hex to binary converter ; ; rugxulo _AT_ gmail ; ; public domain, free for any use, nenies proprajho, "Christus Rex!" VALIDATE=1 ARGV=81h CR=13 LF=10 PUTC=2 WRITESTR=9 DOS equ int 21h GOODBYE equ int 20h b equ byte w equ word ; .text org 100h Komenco mov di,ARGV cmp b di ,CR jz Fino xor ch,ch mov cl,b di-1 inc cl .skiplead mov al,' ' push cx rep scasb pop cx dec di cmp b di ,CR jz Fino mov si,di .skiptrail mov bx,cx lea di,