Printing a character with NASM
Posted: Tue May 03, 2011 8:36 pm
I'm using NASM with Cygwin. I thought that you could print characters using int 21h with NASM, but apparently that's only a DOS command? How do I do it in win32?
The Place to Start for Operating System Developers
https://f.osdev.org/
Code: Select all
#include <stdio.h>
int main(void)
{
FILE *f = fopen("CON", "w");
fputs("hello, world!", f);
fclose(f);
}
Code: Select all
[list -]
%include 'win32n.inc'
[list +]
section .bss use32
section .data use32
section .code use32
cpu 386
extern WriteFile
extern GetStdHandle
extern ExitProcess
import WriteFile kernel32.dll
import GetStdHandle kernel32.dll
import ExitProcess kernel32.dll
section .code
..start:
mov eax,1
push eax
mov eax,1
pop ebx
add eax, ebx
push STD_OUTPUT_HANDLE
call [GetStdHandle] ;Get stdout
mov [stdout_handle], eax ;Save the handle
;Write a message to stdout
push dword 0 ;error code
push dword 0 ;octets_written
push dword textlen ;in octets to write
push dword text1 ;in buffer
push dword [stdout_handle] ;in handle
call [WriteFile] ;Write message
exit:
push dword 0 ;Point at error code
call [ExitProcess]
jmp exit ;Should never reach here
section .data
stdout_handle: dd 0
text1: db 53, 13, 10
textlen: equ $ - text1
That only works if you can guarantee that the C runtime actually prints to the console and not somewhere else.Combuster wrote:If you have to do it the portable way, just write to stdout and have the C runtime take care of the rest.
Indeed, so think of it as pseudocode. The point was writing to the CON file (and maybe use ANSI codes for colors, etc.). According to MSDN, both techniques should work and this one is apparently what the OP decided to go for. However, I'm not sure it's as flexible because NT added all sorts of functions to deal with security and so forth, but at least it works on 9x.Combuster wrote:But the OP did not ask for C code.