I am learning assembly development on Windows 10 (64-bit) and using IntelliJ IDEA to write my assembly code. I’m trying to log multiple outputs using WriteConsoleA
, but only the first log message appears. The second log message is ignored.
First I tried to implement arithmetic logic to get the sum of two numbers. It didn’t work and I tried to hardcode the value and get a log result and then I realized this problem.
Here is my detailed setup and the code I am using.
Environment:
- Operating System: Windows 10 (64-bit)
- Assembler: NASM
- Compiler/Linker: MinGW-Win64
Code:
section .data
sum_msg db 'Sum: ', 0
sum db '15', 0
section .bss
num_bytes_written resq 1
section .text
global _start
extern GetStdHandle
extern WriteConsoleA
extern ExitProcess
_start:
; Get the handle for stdout
mov ecx, -11 ; STD_OUTPUT_HANDLE
call GetStdHandle
; Check if handle is valid
cmp rax, -1
je .exit
; Write the "Sum: " message to stdout
mov rcx, rax ; handle to stdout
lea rdx, [rel sum_msg] ; pointer to message
mov r8d, 5 ; message length
lea r9, [rel num_bytes_written] ; pointer to number of bytes written
call WriteConsoleA
; Write the hardcoded sum to stdout
mov rcx, rax ; handle to stdout
lea rdx, [rel sum] ; pointer to sum string
mov r8d, 2 ; sum string length (including null terminator)
lea r9, [rel num_bytes_written] ; pointer to number of bytes written
call WriteConsoleA
; Infinite loop to prevent immediate exit
.loop:
nop
jmp .loop
.exit:
; Exit
xor ecx, ecx ; exit code 0
call ExitProcess
Commands:
Assemble code:
nasm -f win64 sum.asm -o sum.obj
Link code:
gcc -nostdlib -o sum.exe sum.obj -lkernel32
Expected Output:
Sum: 15
Actual output:
Sum:
Attempts to Fix:
- Verified that the initial message (“Sum: “) logs correctly.
- Added a loop to ensure the program doesn’t exit immediately, confirming that the second log is ignored.
- Tried changing the length of the second message.
- Confirmed that the WriteConsoleA function is called, but it doesn’t print the second message.
- Tested with hardcoded values for the second log message.
- Tried to compile the program by re-ordering the
WriteConsoleA
(top: “15”, bottom: “Sum: “) and it logs the “15” result.
What exactly do I need to know
- Why is the second call to WriteConsoleA not logging the message?
- Is there a specific requirement or constraint for calling WriteConsoleA multiple times in a 64-bit Windows assembly program?
Any help or suggestions would be greatly appreciated.