# a simple hex viewer for linux # by andi näpflin, andi ##at## greyhat ##dot## com # http://www.greyhat.ch # # gcc -o hex hex.s # ./hex # -- have fun!!! # # greetings to markos .section .rodata file_mode: .asciz "rb" ErrorMsg0: .asciz "fopen is failed!" ErrorMsg1: .asciz "fseek is failed!" ErrorMsg2: .asciz "malloc is failed!" ErrorMsg3: .asciz "fread is failed!" ErrUsageMsg: .asciz "Usage: ./hex [http://www.greyhat.ch / Andreas Naepflin]\n" Print_newline: .asciz "\n" Print_hex: .asciz "%02x " Print_line: .asciz "%07x " .section .text .globl main main: pushl %ebp pushl %esi pushl %edi cmpl $2, 16(%esp) jne ErrUsage movl 20(%esp), %eax movl 4(%eax), %eax subl $16, %esp movl $file_mode, 4(%esp) pushl %eax popl (%esp) call fopen testl %eax, %eax jz Error0 movl %eax, %ebp movl %ebp, (%esp) movl $0, 4(%esp) movl $2, 8(%esp) call fseek testl %eax, %eax jnz Error1 #on error -1 movl %ebp, (%esp) call ftell movl %eax, %ebx incl %eax movl %eax, (%esp) call malloc testl %eax, %eax jz Error2 movl %eax, %esi movb $0, (%ebx, %esi, 1) # adding string termination NULL movl %ebp, (%esp) call rewind movl %ebp, 12(%esp) movl $1, 8(%esp) movl %ebx, 4(%esp) movl %esi, (%esp) call fread testl %eax, %eax js Error3 # %ebx -> bufsize # %esi -> buffer # %edi -> counter xorl %edi, %edi movl %edi, 4(%esp) movl $Print_line, (%esp) call printf movl %edi, 8(%esp) loop: movzbl (%esi, %edi, 1), %eax movl %eax, 4(%esp) movl $Print_hex, (%esp) call printf incl %edi testl $0xf, %edi jnz NoNewline movl $Print_newline, (%esp) call printf addl $0x10, 8(%esp) pushl 8(%esp) popl 4(%esp) movl $Print_line, (%esp) call printf NoNewline: cmpl %edi, %ebx jnz loop End0: movl $Print_newline, (%esp) call printf movl %ebp, (%esp) call fclose Exit: movl $0, (%esp) call exit Error0: movl $ErrorMsg0, (%esp) call puts jmp Exit Error1: movl $ErrorMsg1, (%esp) call puts jmp Exit Error2: movl $ErrorMsg2, (%esp) call puts jmp Exit Error3: movl $ErrorMsg3, (%esp) call puts jmp Exit ErrUsage: movl $ErrUsageMsg, (%esp) call printf jmp Exit