Skip to content

Instantly share code, notes, and snippets.

@Kittoes0124
Last active March 7, 2019 20:22
Show Gist options
  • Save Kittoes0124/a1ac61434ef1799a8e723d01e53cbe0d to your computer and use it in GitHub Desktop.
Save Kittoes0124/a1ac61434ef1799a8e723d01e53cbe0d to your computer and use it in GitHub Desktop.
COMMENT @
https://agner.org/optimize/instruction_tables.pdf
https://agner.org/optimize/microarchitecture.pdf
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesizeex
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-readfile
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile
https://docs.microsoft.com/en-us/windows/desktop/api/handleapi/nf-handleapi-closehandle
https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-createfilemappingw
https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-mapviewoffileex
https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-unmapviewoffileex
https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-exitprocess
IACA_BEGIN: mov byte ptr gs:[06fh], 06fh
IACA_END: mov byte ptr gs:[0deh], 0deh
@
include WindowsApi.inc
include WString.inc
CARRIAGE_RETURN = 0Dh
LINE_FEED = 0Ah
QUOTE_DOUBLE = 22h
var0 textequ <rcx>
var1 textequ <rdx>
var2 textequ <r8>
var3 textequ <r9>
var4 textequ <qword ptr [(rsp + 20h)]>
var5 textequ <qword ptr [(rsp + 28h)]>
var6 textequ <qword ptr [(rsp + 30h)]>
.const
align 2
filePath: WString <C:/Temp/Validation.csv>
align 2
stdOutPath: WString <CONOUT$>
.code
Main proc
push r15 ; put the contents of r15 on the stack so that the register can be freely used
push r14 ; put the contents of r14 on the stack so that the register can be freely used
push r13 ; put the contents of r13 on the stack so that the register can be freely used
push rbx ; put the contents of rbx on the stack so that the register can be freely used
sub rsp, 78h ; make room on the stack for the "home space" (0h-20h), function parameters (20h-38h), local variables (38h-58h), and a temporary string (58h-78h)
lea var0, filePath ; put address of filePath into parameter slot 0
mov var1, FILE_ACCESS_READ ; put access mode into parameter slot 1
mov var2, FILE_SHARE_READ ; put share mode into parameter slot 2
xor var3, var3 ; put pointer to security attributes structure into parameter slot 3
mov var4, FILE_DISPOSITION_OPEN ; put disposition into parameter slot 4
mov var5, FILE_FLAG_SEQUENTIAL_SCAN ; put flags into parameter slot 5
mov var6, WINDOWS_NULL ; put pointer to template handle into parameter slot 6
call CreateFile ; create file handle
cmp rax, WINDOWS_INVALID_HANDLE ; validate file handle
je exitMain ; if create validation failed, jump to exitMain
mov qword ptr [(rsp + 38h)], rax ;
mov var0, rax ;
lea var1, qword ptr [(rsp + 50h)] ;
call GetFileSize ;
test rax, rax ;
je closeFileHandle ;
mov var0, qword ptr [(rsp + 38h)] ;
mov var1, WINDOWS_NULL ;
mov var2, 02h ;
mov var3, 0h ;
mov var4, 0h ;
mov var5, WINDOWS_NULL ;
call CreateFileMapping ;
test rax, rax ;
je closeFileHandle ;
mov qword ptr [(rsp + 40h)], rax ;
mov var0, rax ;
mov var1, 04h ;
mov var2, 0h ;
mov var3, 0h ;
mov var4, 0h ;
call MapViewOfFile ;
test rax, rax ;
je closeFileMappingHandle ;
mov qword ptr [(rsp + 48h)], rax ;
;***************************************; (DEBUG INITIALIZATION BEGIN)
lea var0, stdOutPath ; put address of stdOutPath into parameter slot 0
mov var1, FILE_ACCESS_WRITE ; put access mode into parameter slot 1
mov var2, FILE_SHARE_READ ; put share mode into parameter slot 2
xor var3, var3 ; put pointer to security attributes structure into parameter slot 3
mov var4, FILE_DISPOSITION_OPEN ; put disposition into parameter slot 4
mov var5, FILE_FLAG_NONE ; put flags into parameter slot 5
mov var6, WINDOWS_NULL ; put pointer to template handle into parameter slot 6
call CreateFile ; create console out handle
cmp rax, WINDOWS_INVALID_HANDLE ; validate console out handle
je closeMapViewOfFile ; if create validation failed, jump to closeMapViewOfFile
mov r15, rax ; put STDOUT handle into the stdOutHandle variable
;***************************************; (DEBUG INITIALIZATION END)
;---------------------------------------; (FUNCTION INITIALIZATION BEGIN)
mov r14, qword ptr [(rsp + 48h)] ; put pointer to FILE_START into the bufferOffset variable
mov r13, qword ptr [(rsp + 50h)] ; initialize bufferTail variable with the file length
add r13, r14 ; convert bufferTail variable into a pointer to FILE_END by summing it with FILE_START
xor eax, eax ; put zero into the currentLineLength variable
movzx ebx, byte ptr[r14] ; put first character of the file into the nextCharacter variable
xor ecx, ecx ; put null into the currentCharacter variable
xor edx, edx ; put false into the isQuotedSequence variable
;---------------------------------------; (FUNCTION INITIALIZATION END)
;---------------------------------------; (FUNCTION MAIN LOOP BEGIN)
jmp nextChar ; jump to nextChar
hitCarriageReturn: ; define hitCarriageReturn label
cmp ebx, LINE_FEED ; validate that the next character isn't LINE_FEED
jz nextChar ; if the next character is LINE_FEED, jump to nextChar
hitLineFeed: ; define hitLineFeed label
cmp edx, 1h ; validate that we're not currently in a quoted sequence
jz nextChar ; if in a quoted sequence, jump to nextChar
;***************************************; (DEBUG OUTPUT BEGIN)
mov var0, r15 ;
mov var1, r14 ;
sub var1, rax ;
mov var2, rax ;
lea var3, qword ptr [(rsp + 50h)] ;
mov var4, 0h; ;
call WriteFile ;
;***************************************; (DEBUG OUTPUT END)
xor eax, eax ; put zero into the currentLineLength variable
xor edx, edx ; put false into the isQuotedSequence variable
jmp nextChar ; jump to nextChar
hitQuoteDouble: ; define hitQuoteDouble label
xor edx, 1h ; invert the isQuotedSequence variable
nextChar: ; define nextChar label
add eax, 1h ; increment the currentLineLength variable
add r14, 1h ; increment the bufferOffset variable
cmp r14, r13 ; validate that there are more characters to read
jz endOfFile ; if end of file reached, jump to endOfFile
mov ecx, ebx ; put the current character into the currentCharacter variable
movzx ebx, byte ptr[r14] ; put the next character into the nextCharacter variable
cmp ecx, CARRIAGE_RETURN ; if current character is CARRIAGE_RETURN, jump to hitCarriageReturn
jz hitCarriageReturn ; check if the current character is CARRIAGE_RETURN
cmp ecx, LINE_FEED ; if current character is LINE_FEED, jump to hitLineFeed
jz hitLineFeed ; check if the current character is LINE_FEED
cmp ecx, QUOTE_DOUBLE ; if current character is QUOTE_DOUBLE, jump to hitQuoteDouble
jz hitQuoteDouble ; check if the current character is QUOTE_DOUBLE
jmp nextChar ; jump to nextChar
endOfFile: ; define endOfFile label
; TODO: implement logic that handles the last line
;---------------------------------------; (FUNCTION MAIN LOOP END)
;---------------------------------------; (FUNCTION CLEAN UP BEGIN)
closeConsoleOutHandle: ; define closeConsoleOutHandle label
mov var0, r15 ; put STDOUT handle into parameter slot 0
call CloseHandle ; close STDOUT handle
closeMapViewOfFile: ; define closeMapViewOfFile label
mov var0, qword ptr [(rsp + 48h)] ; put map view of file address into parameter slot 0
mov var1, 0h ; put unmap flag(s) into parameter slot 1
call UnmapViewOfFile ; unmap view of file
closeFileMappingHandle: ; define closeFileMappingHandle label
mov var0, qword ptr [(rsp + 40h)] ; put file mapping handle into parameter slot 0
call CloseHandle ; close file mapping handle
closeFileHandle: ; define closeFileHandle label
mov var0, qword ptr [(rsp + 38h)] ; put file handle into parameter slot 0
call CloseHandle ; close file handle
exitMain: ; define exitMain label
add rsp, 78h ; free up all space on the stack
pop rbx ; restore the original contents of rbx
pop r13 ; restore the original contents of r13
pop r14 ; restore the original contents of r14
pop r15 ; restore the original contents of r15
sub rsp, 8h ; align with 16
xor ecx, ecx ; set return value to zero
call ExitProcess ; return control to Windows
;---------------------------------------; (FUNCTION CLEAN UP END)
Main endp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment