2
votes

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 :)

2
Interrupt 21h is a DOS call and thus will function under DOS. I suggest that you open a DOS window then run your program from there in order to see the output.No'am Newman
A 32-bit Windows system will run a 16-bit DOS program, so you can use the Ray Duncan version if that'll suit you. However, 64-bit Windows systems no longer have the MS-DOS subsystem/emulation so it won't work there. You'll need to use the Windows APIs, or if you're more adventurous the native NT APIs (which are pretty similar, but generally not documented). If you want to use the actual system function dispatcher, take a look at Russinovich's "Windows Internals" book - the actual system dispatch mechanism used is very processor and Windows version dependent.Michael Burr

2 Answers

2
votes

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
-2
votes

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