It is from Chapter 10 of jeff duntemann's book on assembly language. I tried to write some code by myself but I can't get it to work properly. I need some insight please.
;Library Name:hexasc_lib;used as library for hexasc.asm
section .data
DumpLin: db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
DumpLen equ $-DumpLin
AscLin: db "|..............|",10
AscLen equ $-AscLin
HexDigits: db "0123456789ABCDEF"
Dot:
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 20h,21h,22h,23h,24h,25h,26h,27h,28h,29h,2Ah,2Bh,2Ch,2Dh,2Eh,2Fh
db 30h,31h,32h,33h,34h,35h,36h,37h,38h,39h,3Ah,3Bh,3Ch,3Dh,3Eh,3Fh
db 40h,41h,42h,43h,44h,45h,46h,47h,48h,49h,4Ah,4Bh,4Ch,4Dh,4Eh,4Fh
db 50h,51h,52h,53h,54h,55h,56h,57h,58h,59h,5Ah,5Bh,5Ch,5Dh,5Eh,5Fh
db 60h,61h,62h,63h,64h,65h,66h,67h,68h,69h,6Ah,6Bh,6Ch,6Dh,6Eh,6Fh
db 70h,71h,72h,73h,74h,75h,76h,77h,78h,79h,7Ah,7Bh,7Ch,7Dh,7Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
db 2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh,2Eh
section .text
GLOBAL ClearLine,DumpChar,PrintLine
;------------------------------------------------------------------------------------------------------------
;ClearLine:Clear a hex dump line string to 16 0 values
;Updated:
;In:
;OUT:
;MODIFIES:edx
;CALLS:DumpChar
;Description: The hex dump line string is cleared to a binary 0.
ClearLine: push edx
mov edx,15
.Poke: mov eax,0 ;poke a NULL
call DumpChar
sub edx,1
;cmp edx,1 ; 1~16
jae .Poke
pop edx
ret
;----------------------------------------------------------------------------------------------------------------
;DumpChar:Inserts a value into the hex dump line string
;Updated:
;In: Value to be poked into the line string is in eax, position in line string where character is to be poked in is in edx
;OUT:
;MODIFIES:eax
;CALLS:
;Description:
DumpChar:
push ebx
push edi
;First insert the input char into the ASCII portion of the dump line
mov bl,byte [Dot+eax]
mov byte [AscLin+edx+1],bl
mov ebx,eax ;save a second copy of the input character
lea edi,[edx*2+edx]
;Translate the input character into its corresponding hex digit
;extract lowest nybble and convert to hex digit
mov al,byte [HexDigits+eax]
and al,0fh
mov byte [DumpLin+edi+2],al
;extract highest nybble and convert into corresponding hex digit
and ebx,000000f0h;mask all bits except the highest 4 bits in bl (removes any additional bits that might be there in the higher 24 bits)
shr bl,4
mov bl,byte [HexDigits+ebx]
mov byte [DumpLin+edi+1],bl
pop edi
pop ebx
ret
;----------------------------------------------------------------------------------------------------------------
;PrintLine:Displays the hex dump line
;Updated:
;In:
;OUT:
;MODIFIES:
;CALLS:
;Description: Uses the sys_write call to print a line, needs the address of the string buffer to be in ecx and length to be in edx
PrintLine:
pushad ;push all gp registers
mov eax,4
mov ebx,1
mov ecx,DumpLin
mov edx,DumpLen
Int 80h
popad
ret
;Executable:hexasc
section .bss
BUFFLEN equ 10 ;read 10 characters from file at a time, but print out 16 entries per line
Buff: resb BUFFLEN
section .data
section .text
EXTERN ClearLine,DumpChar,PrintLine
global _start
_start:
xor esi,esi ;clears total chars (in line) counter to 0
Read: mov eax,3
mov ebx,0
mov ecx,Buff
mov edx,BUFFLEN
INT 80H
mov ebp,eax
cmp eax,0
je Exit
xor ecx,ecx ;clear buffer pointer to 0
xor edx,edx
Scan: xor eax,eax
mov al, byte [Buff+ecx] ;get a char from buffer to al
mov edx,esi ;move the number of chars in counter to edx
call DumpChar
inc ecx
inc esi
cmp ecx,ebp
jae Read
;Test if we are at the end of a block of 16 entries and need to display a line
test esi,0000000fh ;if the lower 4 bits are all 1, then esi contains a value of of mod 16(0-15).Thus, ZF=0
jnz Scan ; if ZF=1, then the lower 4 bits are not 1,hence loop back
;ecx contains Char#, esi contains number of characters on line so far
call PrintLine
call ClearLine
jmp Read ; refill buffer with more data
Exit:
call PrintLine
mov eax,1
mov ebx,0
INT 80H
;makefile
hexasc:hexasc.o hexasc_lib.o
ld -o hexasc hexasc.o hexasc_lib.o
hexasc.o:hexasc.asm
nasm -f elf -g -F stabs hexasc.asm
hexasc_lib.o:hexasc_lib.asm
nasm -f elf -g -F stabs hexasc_lib.asm
I hope I can get some help this time. Last time I post one thread, there was no reply. :(