This is a file dump program written in assembly that displays 320 bytes at a time in 20 lines. It shows the hex code for each character and also the string
At the start of the program it asks for the name of the file and starts dumping. After each dump it asks if they want to see more.
x86 64bit NASM
%macro input 1
push rax
push rdi
push rsi
push rdx
mov eax, SYSREAD
mov edi, 1
mov esi, buffer2
mov edx, 20
syscall
mov esi, buffer2
mov edi, %1
; this loop makes sure the string is the correct size
%%loop:
cmp byte [esi], 0
je %%exitinput
cmp byte [esi], 10
je %%exitinput
movsb
jmp %%loop
%%exitinput:
pop rdx
pop rsi
pop rdi
pop rax
%endmacro
%macro print 2
push rdi
push rax
push rdi
push rdx
push rsi
push rcx
mov esi, %1
mov eax, SYSWRITE
mov edi, 1
mov edx, %2
syscall
pop rcx
pop rsi
pop rdx
pop rdi
pop rax
pop rdi
%endmacro
%macro exit 0
mov rax, 60
mov rdi, 0
syscall
%endmacro
section .bss
hex resb 2
alert resb 16
char resb 1
filename resb 20
buffer resb 320
SYSREAD equ 0
SYSWRITE equ 1
SYSOPEN equ 2
section .data
TABLE db "0123456789ABCDEF", 0
log db "Show more? (y/n)", 10, 0
loglen equ $ -log
section .text
global _start
_start:
input filename
mov eax, 2
mov rdi, filename
mov rsi, 0
mov edx, 0777
syscall
mov ebx, TABLE
push rax
program:
mov eax, SYSREAD
pop rdi
push rdi
mov esi, buffer
mov edx, 320
syscall
; check for end of file
cmp eax, 0
je eof
mov edi, buffer
; print 20 lines
mov ecx, 20
printer:
call displayhex
print space, 1
print alert, 16
print newline, 1
loop printer
print log, loglen
input char
cmp byte [char], "y"
je program
eof:
exit
displayhex:
push rcx
; string characters stored here
mov esi, alert
; get 16 characters from file
mov ecx, 16
loop:
push rcx
mov al, [edi]
cmp al, 10
je addspace
cmp al, 13
je continue
mov [esi], al
jmp continue
addspace:
mov byte [esi], 32
continue:
; convert ascii decimal to ascii hex
mov cx, 16
cwd
div cx
push rax
mov al, dl
xlat
mov [hex + 1], al
pop rax
cwd
div cx
mov al, dl
xlat
mov [hex], al
print space, 1
print hex, 2
end:
inc esi
inc edi
pop rcx
dec ecx
cmp ecx, 0
jnz loop
pop rcx
ret