Here is the code with code blocks.
I prettied it up a bit.
Ed Eberle
; Edward C. Eberle
; San Diego, California USA
; eberdeed@pacbell.net
; Language nasm (the Netwide Assembler)
; Created on a 64 bit Debian Linux System.
; Distributed under the GNU License.
; Welcome to 64 bit registries and stuff.
; This program echos back what is written at the
; command line. It also produces a text file
; that is a copy of what was writen during
; the session.
; The rules:
; 1. All offsets from a memory location (read label)
; require a 32 bit register (begins with an e eax, ebx...)
; 2. All characters get a quadword for storage.
; True because the Linux used accepts UTF-8 encoding.
BITS 64 ; Default to 64 bit alignment
; Also requires quadword.
GLOBAL main ; Program entry point
extern fgets ; C routines used to do the input and output.
extern fgetc
extern fopen
extern fputc
extern putchar
extern fputs
extern puts
extern getc
extern getchar
extern fclose
extern clearerr
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Data Storage (Variables)
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SECTION .bss
txtstorage: resq 0xFFFF
message: resq 0x100
varfilename: resq 0x100
varlenfilename: resq 0x01
varfilepointer: resq 0x01
docsize: resq 0x01
varfileaccess: resq 0x04
strlen: resq 0x01
tmpchr: resq 0x01
offset: resq 0x01
countr: resd 0x01
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Fixed Data (Strings)
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SECTION .data
fileerrorstr: dq "The program has had an Input -- Output error.",0x00
lenfileerrorstr: dd $-fileerrorstr
fatalerror: dq "The program has made a fatal error. END PROGRAM.",0x00
lenfatalerror: dd $-fatalerror
samplestr: dq "A good day!",0x00 ;The day.
lensamplestr: dd $-samplestr
messagesize: equ 0x100
standardout: equ 0x01 ;stdout
standardin: equ 0x00 ;stdin
streamclose: equ 0x06
streamread: equ 0x03
streamwrite: equ 0x04 ;Talking to the colonel
txtstoragesize: equ 0xFFFF ;size of storage.
mydspace: dq 0x0A,0x0A,0x00 ; double space output
lenmydspace: dq $-mydspace
spacestring: dq 0x09,0x00 ; tab over from the left.
lenspacestr: dq $-spacestring
myfilestring: db "Enter a file name and path: ",0x00 ;String data.
lenmyfilestr: dq $-myfilestring ;Get the length of the string.
ferrorstring: dq "!!Buffer Overrun!! ",0x00 ;String data.
lenferrorstr: dq $-ferrorstring ;Get the length of the string.
fconfstring: dq "You have entered the file name: ",0x00 ;String data.
lenfconfstr: dq $-fconfstring ;Get the length of the string.
fileattribs: dq "w",0x00 ;write to a file.
lenfileattribs: dq $-fileattribs
fileattribsrd: dq "r",0x00 ;read a file.
lenfileattribsrd: dq $-fileattribs
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Beginning of executable code.
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
SECTION .text
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Printing Strings
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
main: mov rcx, 0x00 ; a simple loop to initialize storage.
.clearstr: mov byte [varfilename + rcx], 0x00
inc rcx
mov rdx, 0xFF
cmp rcx,rdx
jne .clearstr
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify command line output.
MOV rcx, mydspace ; Load the location of the string.
MOV rdx, [lenmydspace] ;Load the length of the string.
INT 0x80 ` ; Call the operating system using an interrupt.
MOV rax, streamwrite ; Repeat as needed.
MOV rbx, standardout
MOV rcx, spacestring
MOV rdx, [lenspacestr]
INT 0x80 ; Call the kernel
MOV rax, streamwrite
MOV rbx, standardout
MOV rcx, myfilestring ; Load the location of the string.
MOV rdx, [lenmyfilestr]
INT 0x80 ; Call the operating system using an interrupt.
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Command Line Reads
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MOV dword [varlenfilename], 0x00 ; Initialize the length of the string.
.loop1: CALL getchar ; Get a character
mov [tmpchr], eax
mov ebx, [varlenfilename] ; put the string length in a register.
mov [varfilename + ebx], eax ;put the character in the memory.
INC ebx ;increment the index for the string.
MOV [varlenfilename], ebx ;put the index in memory
mov ecx, [tmpchr]
mov eax, 0x0A ; move a line feed into a register.
cmp eax, ecx ; compare the two values.
jne .loop1 ; If not end of line go get another character.
mov ebx, [varlenfilename]
dec ebx
mov byte [varfilename + ebx], 0x00
inc ebx
mov [varlenfilename], ebx
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Printing Strings
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Double space the line.
.next1: MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, mydspace ; Load the location of the string.
MOV rdx, [lenmydspace] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt. ;Indent the line.
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, spacestring ; Load the location of the string.
MOV rdx, [lenspacestr] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
; Output an explanation string.
MOV rax, streamwrite
MOV rbx, standardout
MOV rcx, fconfstring
MOV rdx, [lenfconfstr]
INT 0x80 ;Output our newly captured string.
MOV rax, streamwrite
MOV rbx, standardout
MOV rcx, varfilename
MOV rdx, [varlenfilename]
INT 0x80
; Double space.
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, mydspace ; Load the location of the string.
MOV rdx, [lenmydspace] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
; Tab indent
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, spacestring ; Load the location of the string.
MOV rdx, [lenspacestr] ;Load the length of the strret
INT 0x80 ; Call the operating system using an interrupt.
; Using the captured string open a file.
jmp getdata
errorsarea:
.errorname: MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Write to the screen (kernel command).
MOV rcx, ferrorstring ; Load the location of the string.
MOV rdx, [lenferrorstr] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
.fileerror: mov rax, streamwrite
MOV rbx, standardout
MOV rcx, fileerrorstr
MOV rdx, [lenfileerrorstr]
int 0x80
mov rax, [varfilepointer]
call clearerr
mov rbx,rax
mov rax,0x00
cmp rax,rbx
je getdata
mov rax, 0x04
mov rbx, 0x01
mov rcx, fatalerror
mov rdx, lenfatalerror
int 0x80
.endthis: ret
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Read a file with formatting.
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
getdata: mov rcx, 0x00
mov qword [strlen], 0x00
.loop1: MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, mydspace ; Load the location of the string.
MOV rdx, [lenmydspace] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, spacestring ; Load the location of the string.
MOV rdx, [lenspacestr] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, mydspace ; Load the location of the string.
MOV rdx, [lenmydspace] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
MOV rax, streamwrite ; Call the kernel
MOV rbx, standardout ; Specify the console
MOV rcx, spacestring ; Load the location of the string.
MOV rdx, [lenspacestr] ;Load the length of the string.
INT 0x80 ; Call the operating system using an interrupt.
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Command Line Reads
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MOV qword [strlen], 0x00 ; Initialize the length of the string.
.loop2: call getchar ; Get a character.
mov [tmpchr], rax
mov rbx, 0x7E
cmp rbx, rax
je openfile
mov ebx, [strlen]
mov rax, [tmpchr]
mov [ebx + txtstorage], rax
inc ebx
mov [strlen], ebx
jmp .loop2 ; keep reading characters.
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Open a file for writing
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
openfile: MOV edi, varfilename ; Load the file name.
MOV esi, fileattribs ; Load the file attributes
call fopen ; Open the file
mov [varfilepointer], rax ; Save the file pointer.
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Write data
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
.loop1: mov rsi, [varfilepointer]
mov rdi, txtstorage
call fputs
mov rsi, [varfilepointer]
mov rdi, 0x7E
call fputc
.ender: mov rdi, [varfilepointer]
call fclose
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Open and Echo a File
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
openread: mov ecx, 0x00
.clearstr: mov byte [txtstorage + ecx], 0x00
inc ecx
mov edx, 0xFFFE
cmp ecx, edx
jne .clearstr
MOV rdi, varfilename ; Load the file name.
MOV rsi, fileattribsrd ; Load the file attributes
call fopen ; Open the file
mov [varfilepointer], rax ; Save the file pointer
mov qword [offset], 0x00
.loop1: mov edi, [varfilepointer]
call getc
mov [tmpchr], eax
mov ecx, [offset]
mov [txtstorage + ecx], eax
inc ecx
mov [offset], ecx
mov edx, [docsize]
cmp ecx, edx
je .ender
mov edx, 0x0
mov ecx, [tmpchr]
cmp edx, ecx
jge .ender
mov edx, 0x7E
mov ecx, [tmpchr]
cmp edx, ecx
je .ender
mov edi, ecx
call putchar
jmp .loop1
.ender: ret
mov rdi, [varfilepointer]
call fclose
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; Print the data on the console.
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
mov rax, streamwrite
mov rbx, standardout
mov rcx, txtstorage
mov rdx, [docsize]
int 0x80
ret
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; End of file
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~