initial import
[staff/goc4/2018bti7061.git] / asm-6-bits-branching / examples / hexdump1 / hexdump1.asm
1 ; Executable name : hexdump1
2 ; Version : 1.0
3 ; Created date : 4/4/2009
4 ; Last update : 4/4/2009
5 ; Author : Jeff Duntemann
6 ; Description : A simple program in assembly for Linux, using NASM 2.05,
7 ; demonstrating the conversion of binary values to hexadecimal strings.
8 ; It acts as a very simple hex dump utility for files, though without the
9 ; ASCII equivalent column.
10 ;
11 ; Run it this way:
12 ; hexdump1 < (input file)
13 ;
14 ; Build using these commands:
15 ; nasm -f elf64 -g -F stabs hexdump1.asm
16 ; ld -o hexdump1 hexdump1.o
17 ;
18 SECTION .bss ; Section containing uninitialized data
19
20 BUFFLEN equ 16 ; We read the file 16 bytes at a time
21 Buff: resb BUFFLEN ; Text buffer itself
22
23 SECTION .data ; Section containing initialised data
24
25 HexStr: db " 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",10
26 HEXLEN equ $-HexStr
27
28 Digits: db "0123456789ABCDEF"
29
30 SECTION .text ; Section containing code
31
32 global _start ; Linker needs this to find the entry point!
33
34 _start:
35 nop ; This no-op keeps gdb happy...
36
37 ; Read a buffer full of text from stdin:
38 Read:
39 mov eax,3 ; Specify sys_read call
40 mov ebx,0 ; Specify File Descriptor 0: Standard Input
41 mov ecx,Buff ; Pass offset of the buffer to read to
42 mov edx,BUFFLEN ; Pass number of bytes to read at one pass
43 int 80h ; Call sys_read to fill the buffer
44 mov ebp,eax ; Save # of bytes read from file for later
45 cmp eax,0 ; If eax=0, sys_read reached EOF on stdin
46 je Done ; Jump If Equal (to 0, from compare)
47
48 ; Set up the registers for the process buffer step:
49 mov esi,Buff ; Place address of file buffer into esi
50 mov edi,HexStr ; Place address of line string into edi
51 xor ecx,ecx ; Clear line string pointer to 0
52
53 ; Go through the buffer and convert binary values to hex digits:
54 Scan:
55 xor eax,eax ; Clear eax to 0
56
57 ; Here we calculate the offset into the line string, which is ecx X 3
58 mov edx,ecx ; Copy the pointer into line string into edx
59 ; shl edx,1 ; Multiply pointer by 2 using left shift
60 ; add edx,ecx ; Complete the multiplication X3
61 lea edx,[edx*2+edx]
62 ; Get a character from the buffer and put it in both eax and ebx:
63 mov al,byte [esi+ecx] ; Put a byte from the input buffer into al
64 mov ebx,eax ; Duplicate the byte in bl for second nybble
65
66 ; Look up low nybble character and insert it into the string:
67 and al,0Fh ; Mask out all but the low nybble
68 mov al,byte [Digits+eax] ; Look up the char equivalent of nybble
69 mov byte [HexStr+edx+2],al ; Write the char equivalent to line string
70
71 ; Look up high nybble character and insert it into the string:
72 shr bl,4 ; Shift high 4 bits of char into low 4 bits
73 mov bl,byte [Digits+ebx] ; Look up char equivalent of nybble
74 mov byte [HexStr+edx+1],bl ; Write the char equivalent to line string
75
76 ; Bump the buffer pointer to the next character and see if we're done:
77 inc ecx ; Increment line string pointer
78 cmp ecx,ebp ; Compare to the number of characters in the buffer
79 jna Scan ; Loop back if ecx is <= number of chars in buffer
80
81 ; Write the line of hexadecimal values to stdout:
82 mov eax,4 ; Specify sys_write call
83 mov ebx,1 ; Specify File Descriptor 1: Standard output
84 mov ecx,HexStr ; Pass offset of line string
85 mov edx,HEXLEN ; Pass size of the line string
86 int 80h ; Make kernel call to display line string
87 jmp Read ; Loop back and load file buffer again
88
89 ; All done! Let's end this party:
90 Done:
91 mov eax,1 ; Code for Exit Syscall
92 mov ebx,0 ; Return a code of zero
93 int 80H ; Make kernel call