OPTION CASEMAP:NONE GENERIC_READ equ 80000000h GENERIC_WRITE equ 40000000h FILE_SHARE_READ equ 00000001h FILE_SHARE_WRITE equ 00000002h CREATE_NEW equ 1 CREATE_ALWAYS equ 2 OPEN_EXISTING equ 3 OPEN_ALWAYS equ 4 TRUNCATE_EXISTING equ 5 FILE_ATTRIBUTE_NORMAL equ 00000080h INVALID_HANDLE_VALUE equ -1 .data InputPath BYTE "D:/Sorting/Sorting_Input.txt", 0 OutputPath BYTE "D:/Sorting/Output_X64ASM.txt", 0 ProgramName BYTE "========MERGESORT x64 ASM=========", 13, 10, 0 LoadingMessage BYTE "LOADING INPUT FILE...", 0 SavingMessage BYTE "SAVING OUTPUT FILE...", 0 SortingMessage BYTE "SORTING ELEMENT...", 0 DoneMessage BYTE "DONE.", 13,10, 0 CompleteMessage BYTE "==========RUN COMPLETE============", 13, 10, 0 TimeMessage BYTE "MERGESORT TOOK ", 0 SecondsMessage BYTE " SECONDS.", 13, 10, 0 ErrorMessage BYTE "ERROR.", 13, 10, 0 TimerError BYTE "N/A", 0 MAX_VALS equ 1000 INPUT_BUFFER_SIZE equ MAX_VALS * 13 Scratch SDWORD MAX_VALS DUP(?) StartTime QWORD 0 InputBuffer BYTE INPUT_BUFFER_SIZE DUP(?) BytesReadTracker DWORD 0 OverflowProbe BYTE ? .code PUBLIC MergeSort PUBLIC MergeSortMain PUBLIC LoadFile PUBLIC SaveFile PUBLIC WriteConsoleString PUBLIC LoadInput PUBLIC SaveOutput PUBLIC StartTimer PUBLIC StopTimer EXTERN GetStdHandle:PROC EXTERN WriteConsoleA:PROC EXTERN CreateFileA:PROC EXTERN ReadFile:PROC EXTERN CloseHandle:PROC EXTERN WriteFile:PROC EXTERN QueryPerformanceCounter:PROC EXTERN QueryPerformanceFrequency:PROC ; ------------------------------------------------------------------- ; SaveOutput ; Purpose: ; Convert each signed integer to decimal text and write it to a file. ; Parameters: ; Windows x64 calling convention: ; RCX - address of the first 32-bit integer. ; RDX - number of integers to write. ; R8 - null-terminated output file path. ; Returns: ; EAX - 1 on success, 0 if opening, writing, or closing fails. ; Side effects: ; Creates or replaces the output file and writes one integer per line. ; ------------------------------------------------------------------- SaveOutput PROC ; Allocate 152 bytes frame. 8 (ret addr) + 152 = 160 bytes (16-byte aligned) SUB rsp, 152 ; Protect non-volatile registers safely shifted up by 8 bytes MOV QWORD PTR [rsp+88], rbx MOV QWORD PTR [rsp+96], rsi MOV QWORD PTR [rsp+104], rdi MOV QWORD PTR [rsp+112], r12 MOV QWORD PTR [rsp+120], r13 MOV QWORD PTR [rsp+128], r14 MOV QWORD PTR [rsp+136], r15 MOV rbx, rcx ; cursor into the value array MOV r12, rdx ; remaining element count TEST r12, r12 JZ SaveOpenFile TEST rbx, rbx JZ SaveOpenError ; --- Open file --- SaveOpenFile: MOV rcx, r8 ; lpFileName for CreateFileA MOV rdx, GENERIC_WRITE MOV r8, FILE_SHARE_READ MOV r9, 0 ; lpSecurityAttributes (NULL) MOV QWORD PTR [rsp+32], CREATE_ALWAYS MOV QWORD PTR [rsp+40], FILE_ATTRIBUTE_NORMAL MOV QWORD PTR [rsp+48], 0 MOV QWORD PTR [rsp+56], 0 CALL CreateFileA ; Cache file handle cleanly at [rsp+144] MOV QWORD PTR [rsp+144], rax CMP QWORD PTR [rsp+144], INVALID_HANDLE_VALUE JE SaveOpenError WriteLoop: TEST r12, r12 JE CloseOut LEA r14, [rsp+78] ; Start at the back end of stack scratch space MOV byte PTR [r14], 0Ah ; Pre-fill LF DEC r14 MOV byte PTR [r14], 0Dh ; Pre-fill CR MOV r9d, 2 ; Clear length tracking counter to 2 (CR + LF) MOV eax, dword PTR [rbx] ; current element value ; Check for zero TEST eax, eax JNE CheckNegative DEC r14 MOV byte PTR [r14], '0' INC r9d JMP FinishNum CheckNegative: XOR r15d, r15d ; r15d = 0 means positive CMP eax, 0 JGE Convert MOV r15d, 1 ; Mark as negative NEG eax ; Convert negative number to positive Convert: MOV ecx, 10 ; divisor (base 10) ConvLoop: XOR edx, edx DIV ecx ; eax:edx / ecx -> eax = quotient, edx = remainder ADD dl, '0' ; Convert digit to ASCII DEC r14 ; Move text cursor backward MOV byte PTR [r14], dl INC r9d ; Increment string length TEST eax, eax JNE ConvLoop ; Append negative sign if needed CMP r15d, 1 JNE FinishNum DEC r14 MOV byte PTR [r14], '-' ; Append minus sign INC r9d FinishNum: MOV rsi, r14 ; Current string cursor MOV r15d, r9d ; Bytes remaining in this line WriteLine: MOV rcx, QWORD PTR [rsp+144] ; 1st arg: Load safe file handle MOV rdx, rsi ; 2nd arg: current string position MOV r8d, r15d ; 3rd arg: bytes remaining LEA r9, [rsp+80] ; 4th arg: bytes written MOV DWORD PTR [rsp+80], 0 MOV QWORD PTR [rsp+32], 0 ; 5th arg: lpOverlapped CALL WriteFile TEST eax, eax JZ SaveWriteError MOV eax, DWORD PTR [rsp+80] TEST eax, eax JZ SaveWriteError ADD rsi, rax SUB r15d, eax JNZ WriteLine ADD rbx, 4 ; Move to next element DEC r12 JMP WriteLoop CloseOut: MOV rcx, QWORD PTR [rsp+144] CALL CloseHandle TEST eax, eax JZ SaveCloseError MOV eax, 1 JMP SaveDone SaveOpenError: XOR eax, eax JMP SaveDone SaveWriteError: MOV rcx, QWORD PTR [rsp+144] CALL CloseHandle XOR eax, eax JMP SaveDone SaveCloseError: XOR eax, eax SaveDone: ; Restore non-volatile tracking states cleanly MOV rbx, QWORD PTR [rsp+88] MOV rsi, QWORD PTR [rsp+96] MOV rdi, QWORD PTR [rsp+104] MOV r12, QWORD PTR [rsp+112] MOV r13, QWORD PTR [rsp+120] MOV r14, QWORD PTR [rsp+128] MOV r15, QWORD PTR [rsp+136] ADD rsp, 152 RET SaveOutput ENDP ; ------------------------------------------------------------------- ; LoadInput ; Purpose: ; Read and parse signed decimal integers from a file into a caller buffer. ; Parameters: ; Windows x64 calling convention: ; RCX - destination array for 32-bit integers. ; RDX - maximum number of integers the array can hold. ; R8 - null-terminated input file path. ; Returns: ; RAX - number of parsed integers, or -1 on an argument, file, or parse error. ; Side effects: ; Reads and closes the input file and stores parsed integers in the destination array. ; ------------------------------------------------------------------- LoadInput PROC ; Allocate 120 bytes frame. 8 (ret addr) + 120 = 128 bytes (16-byte aligned) SUB rsp, 120 ; Shifted tracking spills down by 8 bytes to match the new 120-byte layout MOV QWORD PTR [rsp+64], rbx MOV QWORD PTR [rsp+72], rsi MOV QWORD PTR [rsp+80], rdi MOV QWORD PTR [rsp+88], r12 MOV QWORD PTR [rsp+96], r13 MOV QWORD PTR [rsp+104], r14 MOV QWORD PTR [rsp+112], r15 XOR eax, eax TEST rdx, rdx JZ LoadDone TEST rcx, rcx JZ LoadArgumentError TEST r8, r8 JZ LoadArgumentError MOV r13, rcx ; r13 = output array base MOV rbx, rdx ; rbx = max element count ; --- Open file --- MOV rcx, r8 ; 1st arg: lpFileName MOV rdx, GENERIC_READ ; 2nd arg: dwDesiredAccess MOV r8, FILE_SHARE_READ ; 3rd arg: dwShareMode MOV r9, 0 ; 4th arg: lpSecurityAttributes (NULL) MOV QWORD PTR [rsp+32], OPEN_EXISTING MOV QWORD PTR [rsp+40], FILE_ATTRIBUTE_NORMAL MOV QWORD PTR [rsp+48], 0 CALL CreateFileA MOV QWORD PTR [rsp+48], rax ; Keep handle across API calls CMP QWORD PTR [rsp+48], INVALID_HANDLE_VALUE JE LoadErrorExit MOV DWORD PTR [BytesReadTracker], 0 ReadMore: MOV eax, INPUT_BUFFER_SIZE SUB eax, DWORD PTR [BytesReadTracker] JZ CheckBufferLimit MOV ecx, DWORD PTR [BytesReadTracker] LEA rdx, InputBuffer ADD rdx, rcx MOV rcx, QWORD PTR [rsp+48] MOV r8d, eax LEA r9, [rsp+56] MOV DWORD PTR [rsp+56], 0 MOV QWORD PTR [rsp+32], 0 CALL ReadFile TEST eax, eax JZ LoadReadError MOV eax, DWORD PTR [rsp+56] ADD DWORD PTR [BytesReadTracker], eax TEST eax, eax JNZ ReadMore JMP CloseInput CheckBufferLimit: MOV rcx, QWORD PTR [rsp+48] LEA rdx, OverflowProbe MOV r8d, 1 LEA r9, [rsp+56] MOV DWORD PTR [rsp+56], 0 MOV QWORD PTR [rsp+32], 0 CALL ReadFile TEST eax, eax JZ LoadReadError CMP DWORD PTR [rsp+56], 0 JNE LoadReadError CloseInput: MOV rcx, QWORD PTR [rsp+48] CALL CloseHandle TEST eax, eax JZ LoadCloseError JMP StartParsing LoadReadError: MOV rcx, QWORD PTR [rsp+48] CALL CloseHandle MOV rax, -1 JMP LoadDone LoadCloseError: MOV rax, -1 JMP LoadDone StartParsing: MOV rax, 0 ; rax = running count of parsed integers LEA r14, InputBuffer ; r14 = tracking cursor pointer MOV r15d, DWORD PTR [BytesReadTracker] TEST r15d, r15d JZ LoadDone ParseLoop: CMP r15d, 0 JE LoadDone ; Skip separators before the next integer; R14 and R15D track buffer position. SkipWS: CMP byte PTR [r14], 0Dh JE NextChar CMP byte PTR [r14], 0Ah JE NextChar CMP byte PTR [r14], ' ' JE NextChar CMP byte PTR [r14], 9 JE NextChar CMP byte PTR [r14], 11 JE NextChar CMP byte PTR [r14], 12 JE NextChar JMP ParseNum NextChar: INC r14 DEC r15d JMP ParseLoop ; Start a signed integer parse, accumulating decimal digits in R8. ParseNum: XOR r8d, r8d ; Clear the digit accumulator XOR r12d, r12d ; r12d = 0 means positive, 1 means negative XOR r11d, r11d ; Number of digits parsed MOV r10d, 80000000h CMP byte PTR [r14], '-' JNE DigitLoop MOV r12d, 1 ; Mark the number as negative INC r14 DEC r15d JZ ParseError ; Consume digits while checking that the value fits in a signed 32-bit integer. DigitLoop: CMP r15d, 0 JE CheckSignAndStore MOVZX edx, byte PTR [r14] CMP dl, '0' JB CheckSignAndStore CMP dl, '9' JA CheckSignAndStore SUB edx, '0' IMUL r8, r8, 10 ADD r8, rdx CMP r8, r10 JA ParseError INC r11d INC r14 DEC r15d JMP DigitLoop ; Require at least one digit, apply the sign, then store one parsed integer. CheckSignAndStore: TEST r11d, r11d JZ ParseError TEST r12d, r12d JNZ ApplyNegative CMP r8, 2147483647 JA ParseError JMP StoreNum ApplyNegative: CMP r12d, 1 JNE StoreNum NEG r8 StoreNum: MOV dword PTR [r13 + rax*4], r8d INC rax CMP rax, rbx JAE LoadDone JMP ParseLoop ParseError: MOV rax, -1 JMP LoadDone LoadErrorExit: LoadArgumentError: MOV rax, -1 LoadDone: MOV rbx, QWORD PTR [rsp+64] MOV rsi, QWORD PTR [rsp+72] MOV rdi, QWORD PTR [rsp+80] MOV r12, QWORD PTR [rsp+88] MOV r13, QWORD PTR [rsp+96] MOV r14, QWORD PTR [rsp+104] MOV r15, QWORD PTR [rsp+112] ADD rsp, 120 RET LoadInput ENDP ; ------------------------------------------------------------------- ; LoadFile ; Purpose: ; Display input progress and load integers from the configured input file. ; Parameters: ; Windows x64 calling convention: ; RCX - destination array for 32-bit integers. ; RDX - maximum number of integers the array can hold. ; Returns: ; RAX - number of integers loaded, or -1 on failure. ; Side effects: ; Prints progress and writes loaded integers into the caller's array. ; ------------------------------------------------------------------- LoadFile PROC SUB rsp, 56 MOV QWORD PTR [rsp+32], rbx ; Save rbx safely in shadow spill area MOV QWORD PTR [rsp+40], rsi ; Save rsi safely MOV rbx, rcx ; Cache array base MOV rsi, rdx ; Cache max count LEA rcx, LoadingMessage CALL WriteConsoleString MOV rcx, rbx MOV rdx, rsi MOV r8, OFFSET InputPath CALL LoadInput MOV rsi, rax TEST rsi, rsi JS LoadFileError LEA rcx, DoneMessage JMP LoadFileStatus LoadFileError: LEA rcx, ErrorMessage LoadFileStatus: CALL WriteConsoleString MOV rax, rsi MOV rbx, QWORD PTR [rsp+32] MOV rsi, QWORD PTR [rsp+40] ADD rsp, 56 RET LoadFile ENDP ; ------------------------------------------------------------------- ; SaveFile ; Purpose: ; Display output progress and write integers to the configured output file. ; Parameters: ; Windows x64 calling convention: ; RCX - address of the first 32-bit integer. ; RDX - number of integers to write. ; Returns: ; RAX - 1 on success, 0 if writing fails. ; Side effects: ; Prints progress and writes the caller's integers to the output file. ; ------------------------------------------------------------------- SaveFile PROC SUB rsp, 56 MOV QWORD PTR [rsp+32], rbx MOV QWORD PTR [rsp+40], rsi MOV rbx, rcx ; Cache array base MOV rsi, rdx ; Cache element count LEA rcx, SavingMessage CALL WriteConsoleString MOV rcx, rbx MOV rdx, rsi MOV r8, OFFSET OutputPath CALL SaveOutput MOV rsi, rax TEST rsi, rsi JZ SaveFileError LEA rcx, DoneMessage JMP SaveFileStatus SaveFileError: LEA rcx, ErrorMessage SaveFileStatus: CALL WriteConsoleString MOV rax, rsi MOV rbx, QWORD PTR [rsp+32] MOV rsi, QWORD PTR [rsp+40] ADD rsp, 56 RET SaveFile ENDP ; ------------------------------------------------------------------- ; MergeSortMain ; Purpose: ; Validate the requested capacity, load values, sort them, and save output. ; Parameters: ; Windows x64 calling convention: ; RCX - destination array for 32-bit integers. ; RDX - capacity of that array; must not exceed MAX_VALS. ; Returns: ; RAX - -1 for invalid arguments or load failure; the current code also ; leaves -1 after the save call. ; Side effects: ; Loads and sorts the array, writes output, and prints status messages. ; ------------------------------------------------------------------- MergeSortMain PROC PUSH rbp MOV rbp, rsp AND rsp, -16 SUB rsp, 64 MOV QWORD PTR [rsp+32], rcx MOV QWORD PTR [rsp+40], rdx CMP rdx, MAX_VALS JA MergeSortMainError TEST rdx, rdx JZ MergeSortMainValidated TEST rcx, rcx JZ MergeSortMainError MergeSortMainValidated: LEA rcx, ProgramName CALL WriteConsoleString MOV rcx, QWORD PTR [rsp+32] MOV rdx, QWORD PTR [rsp+40] CALL LoadFile TEST rax, rax JS MergeSortMainExit MOV QWORD PTR [rsp+48], rax MOV rcx, QWORD PTR [rsp+32] LEA rdx, Scratch MOV r8, QWORD PTR [rsp+48] CALL MergeSort MOV rcx, QWORD PTR [rsp+32] MOV rdx, QWORD PTR [rsp+48] CALL SaveFile TEST rax, rax MOV rax, -1 JMP MergeSortMainExit MergeSortMainSaved: LEA rcx, CompleteMessage CALL WriteConsoleString MOV rax, QWORD PTR [rsp+48] JMP MergeSortMainExit MergeSortMainError: LEA rcx, ErrorMessage CALL WriteConsoleString MOV rax, -1 MergeSortMainExit: MOV rsp, rbp POP rbp RET MergeSortMain ENDP ; ------------------------------------------------------------------- ; MergeSort ; Purpose: ; Sort an integer array with iterative, bottom-up merge sort. ; Parameters: ; Windows x64 calling convention: ; RCX - array to sort in place. ; RDX - scratch array with at least R8 32-bit slots. ; R8 - number of values in the arrays. ; Returns: ; No value; sorts the array at RCX in place. ; Side effects: ; Uses the scratch array, displays progress, and prints elapsed time. ; ------------------------------------------------------------------- MergeSort PROC PUSH rbx PUSH rsi PUSH rdi PUSH r12 PUSH r13 PUSH r14 PUSH r15 SUB rsp, 32 MOV r12, rcx MOV r13, rdx MOV r14, r8 LEA rcx, SortingMessage CALL WriteConsoleString CALL StartTimer MOV QWORD PTR [StartTime], rax CMP r14, 1 JBE SortFinished MOV r10, r12 MOV r11, r13 MOV r15, 1 PassLoop: ; Each pass merges adjacent sorted runs; R15 is their current width. XOR ebx, ebx MergeLoop: ; RCX:RSI bound the left run, RDX:RDI the right run, and R8 the output. CMP rbx, r14 JAE PassDone MOV rcx, rbx LEA rax, [rbx+r15] CMP rax, r14 CMOVA rax, r14 MOV rsi, rax LEA rax, [rsi+r15] CMP rax, r14 CMOVA rax, r14 MOV rdi, rax MOV rdx, rsi MOV r8, rbx MergeBoth: CMP rcx, rsi JAE CopyRight CMP rdx, rdi JAE CopyLeft MOV eax, DWORD PTR [r10+rcx*4] MOV r9d, DWORD PTR [r10+rdx*4] CMP eax, r9d JLE TakeLeft TakeRight: MOV DWORD PTR [r11+r8*4], r9d INC rdx INC r8 JMP MergeBoth TakeLeft: MOV DWORD PTR [r11+r8*4], eax INC rcx INC r8 JMP MergeBoth CopyLeft: CMP rcx, rsi JAE MergeComplete MOV eax, DWORD PTR [r10+rcx*4] MOV DWORD PTR [r11+r8*4], eax INC rcx INC r8 JMP CopyLeft CopyRight: CMP rdx, rdi JAE MergeComplete MOV eax, DWORD PTR [r10+rdx*4] MOV DWORD PTR [r11+r8*4], eax INC rdx INC r8 JMP CopyRight MergeComplete: MOV rbx, rdi JMP MergeLoop PassDone: ; The scratch and input buffers swap roles; double the run width next. MOV rax, r10 MOV r10, r11 MOV r11, rax MOV rax, r15 ADD r15, r15 CMP rax, r14 JB PassLoop CMP r10, r12 JE SortFinished XOR rcx, rcx CopyBackLoop: ; Copy the final pass back when it ended in the scratch buffer. CMP rcx, r14 JAE SortFinished MOV eax, DWORD PTR [r10+rcx*4] MOV DWORD PTR [r12+rcx*4], eax INC rcx JMP CopyBackLoop SortFinished: LEA rcx, DoneMessage CALL WriteConsoleString LEA rcx, TimeMessage CALL WriteConsoleString MOV rcx, QWORD PTR [StartTime] CALL StopTimer LEA rcx, SecondsMessage CALL WriteConsoleString ADD rsp, 32 POP r15 POP r14 POP r13 POP r12 POP rdi POP rsi POP rbx RET MergeSort ENDP ; ------------------------------------------------------------------- ; WriteConsoleString ; Purpose: ; Write one null-terminated string to the standard output console. ; Parameters: ; Windows x64 calling convention: ; RCX - address of the null-terminated string. ; Returns: ; No meaningful value. ; Side effects: ; Writes the string to the standard output console. ; ------------------------------------------------------------------- WriteConsoleString PROC SUB rsp, 56 MOV QWORD PTR [rsp+40], rcx MOV rcx, -11 ; STD_OUTPUT_HANDLE CALL GetStdHandle MOV r9, rax MOV rdx, QWORD PTR [rsp+40] XOR r10d, r10d CountLoop: CMP byte PTR [rdx], 0 JE WriteIt INC rdx INC r10d JMP CountLoop WriteIt: MOV rcx, r9 MOV rdx, QWORD PTR [rsp+40] MOV r8d, r10d LEA r9, [rsp+48] MOV DWORD PTR [rsp+48], 0 MOV QWORD PTR [rsp+32], 0 CALL WriteConsoleA ADD rsp, 56 RET WriteConsoleString ENDP ; ------------------------------------------------------------------- ; StartTimer ; Purpose: ; Read the high-resolution performance counter. ; Parameters: ; None. ; Returns: ; RAX - counter value, or -1 if the counter cannot be read. ; Side effects: ; Calls the Windows performance-counter API. ; ------------------------------------------------------------------- StartTimer PROC SUB rsp, 40 ; Shadow space, local counter, and alignment LEA rcx, [rsp+32] CALL QueryPerformanceCounter TEST eax, eax JZ StartTimerError MOV rax, QWORD PTR [rsp+32] ADD rsp, 40 RET StartTimerError: MOV rax, -1 ADD rsp, 40 RET StartTimer ENDP ; ------------------------------------------------------------------- ; StopTimer ; Purpose: ; Convert the elapsed counter interval into seconds and print it. ; Parameters: ; Windows x64 calling convention: ; RCX - counter value returned by StartTimer. ; Returns: ; RAX - -1 on timer failure; otherwise its value is not used by callers. ; Side effects: ; Reads timer data and writes the formatted elapsed time to the console. ; ------------------------------------------------------------------- StopTimer PROC SUB rsp, 168 MOV QWORD PTR [rsp+112], rbx MOV QWORD PTR [rsp+120], rsi MOV QWORD PTR [rsp+128], rdi MOV QWORD PTR [rsp+136], r12 MOV QWORD PTR [rsp+144], r13 MOV QWORD PTR [rsp+152], r14 MOV QWORD PTR [rsp+160], r15 MOV rbx, rcx CMP rbx, -1 JE TimerFailure LEA rcx, [rsp+80] CALL QueryPerformanceCounter TEST eax, eax JZ TimerFailure MOV rax, QWORD PTR [rsp+80] SUB rax, rbx MOV QWORD PTR [rsp+64], rax LEA rcx, [rsp+88] MOV QWORD PTR [rsp+88], 0 CALL QueryPerformanceFrequency TEST eax, eax JZ TimerFailure MOV rcx, QWORD PTR [rsp+88] TEST rcx, rcx JZ TimerFailure ; Divide counter ticks into whole seconds and a remainder. MOV rax, QWORD PTR [rsp+64] XOR rdx, rdx DIV rcx MOV QWORD PTR [rsp+96], rax MOV QWORD PTR [rsp+104], rdx ; Convert the fractional tick remainder to nanoseconds before printing. MOV rax, QWORD PTR [rsp+104] MOV rcx, 1000000000 MUL rcx MOV rcx, QWORD PTR [rsp+88] DIV rcx MOV QWORD PTR [rsp+104], rax LEA r12, [rsp+32] MOV rax, QWORD PTR [rsp+96] XOR r15d, r15d TimerSecondsDigits: XOR edx, edx MOV rcx, 10 DIV rcx ADD dl, '0' MOV BYTE PTR [r12], dl INC r12 INC r15d TEST rax, rax JNZ TimerSecondsDigits LEA rsi, [rsp+32] LEA rdi, [r12-1] TimerReverseSeconds: CMP rsi, rdi JAE TimerAppendDecimal MOV al, BYTE PTR [rsi] MOV dl, BYTE PTR [rdi] MOV BYTE PTR [rsi], dl MOV BYTE PTR [rdi], al INC rsi DEC rdi JMP TimerReverseSeconds TimerAppendDecimal: MOV BYTE PTR [r12], '.' INC r12 MOV r13, r12 MOV rax, QWORD PTR [rsp+104] MOV r15d, 9 TimerFractionDigits: XOR edx, edx MOV rcx, 10 DIV rcx ADD dl, '0' MOV BYTE PTR [r12], dl INC r12 DEC r15d JNZ TimerFractionDigits MOV rsi, r13 LEA rdi, [r12-1] TimerReverseFraction: CMP rsi, rdi JAE TimerPrint MOV al, BYTE PTR [rsi] MOV dl, BYTE PTR [rdi] MOV BYTE PTR [rsi], dl MOV BYTE PTR [rdi], al INC rsi DEC rdi JMP TimerReverseFraction TimerPrint: MOV BYTE PTR [r12], 0 LEA rcx, [rsp+32] CALL WriteConsoleString JMP TimerDone TimerFailure: LEA rcx, TimerError CALL WriteConsoleString MOV rax, -1 TimerDone: MOV rbx, QWORD PTR [rsp+112] MOV rsi, QWORD PTR [rsp+120] MOV rdi, QWORD PTR [rsp+128] MOV r12, QWORD PTR [rsp+136] MOV r13, QWORD PTR [rsp+144] MOV r14, QWORD PTR [rsp+152] MOV r15, QWORD PTR [rsp+160] ADD rsp, 168 RET StopTimer ENDP END