Chinmaya Gautam
Chinmaya Gautam

Reputation: 31

hello world in NASM WITHOUT windows api

I've been coding in assembly on Linux using NASM and am now trying to learn the same for Windows. Following advanced MS-DOS programming by Ray Duncan, Figure 3-7 lists a hello world program based on MASM which basically prints "hello world" using interrupt 21h. This is synonymous to doing the same on Linux using interrupt 80h and feels like home. I want to do the same using NASM on windows.

Most of the examples on the net use the Windows API such _GetStdHandle, _WriteConsoleA etc. or use C libraries such as _printf. I want to do it bare bones.Something along the following snippt:

global _start

section .data
    str:     db 'hello, world',0xA
    strLen:  equ $-str

section .text
    _start:

mov ah,40h 
mov bx,1 
mov cx, strLen 
mov dx, str
int 21h 

mov ax,4c00h 
int 21h 

Hope I am not being ambiguous :)

Upvotes: 2

Views: 2991

Answers (2)

Dirk Wolfgang Glomp
Dirk Wolfgang Glomp

Reputation: 539

I like to schow a little variation of the code above from vitsoft, without using software interrupts for printing out a string directly to a given screen coordinate that we have to specify in the code below. (But it do not touch or move the coursor position.) For 64 Bit Windows use DOSBOX.

; Save as hello.asm, assemble with nasm -f bin -o hello.com hello.asm
ORG       256
Start:    JMP Main
strOfs    DB 'hello, world'
strLen    EQU $-strOfs   ; meaning of $ = offset address of this position in the code
Main:     MOV SI,strOfs  ; offset address of the string
          MOV CX,strLen  ; lenght of the string
          MOV AX, 0B800h ; segment address of the textmode video buffer
          MOV ES, AX     ; store the address in the extra segment register
          MOV DI, (Line_Number*80*2)+(Row_number*2) ; target address on the screen
          CLD            ; let the pointer adjustment step forward for string instructions
nextChar: LODSB          ; load AL from DS:[SI], increment SI
          STOSB          ; store AL into ES:[DI], increment DI
          INC DI         ; step over attribute byte
          LOOP nextChar  ; repeat until CX=0
          MOV AH,00h     ; BIOS function GET KEYSTROKE
          INT 16h        ; Press any key to continue
          RET            ; Exit program

Upvotes: 2

vitsoft
vitsoft

Reputation: 5775

If DOS functions are not bare bones enough, you may use BIOS functions hardwired in PC's firmware. They are documented in Ralf Brown's Interrupt List at http://www.ctyme.com/rbrown.htm

; Save as hello.asm, assemble with nasm -f bin -o hello.com hello.asm
ORG       256
Start:    JMP Main
strOfs    DB 'hello, world',0Ah
strLen    EQU $-strOfs
Main:     MOV SI,strOfs
          MOV CX,strLen
          SUB BX,BX ; clear videopage number and color
          MOV AH,0Eh ; BIOS function TELETYPE OUTPUT
          CLD
nextChar: LODSB ; load AL from [SI], increment SI
          INT 10h ; Display one character, advance cursor position
          LOOP nextChar
          MOV AH,00h ; BIOS function GET KEYSTROKE
          INT 16h ; Press any key to continue
          RET     ; Exit program

Upvotes: -2

Related Questions