没有windows api的NASM你好世界

时间:2014-04-03 06:00:25

标签: windows assembly nasm masm

我使用NASM在Linux上进行汇编编码,现在我正在尝试为Windows学习相同内容。在Ray Duncan的高级MS-DOS编程之后,图3-7列出了一个基于MASM的hello world程序,它基本上打印了#hello world"使用中断21h。这是在使用中断80h的Linux上做同样的事情的同义词,感觉像家一样。我想在Windows上使用NASM做同样的事情。

网上的大多数示例都使用Windows API,例如_GetStdHandle,_WriteConsoleA等,或者使用诸如_printf之类的C库。我想做裸露的骨头。以下内容:

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 

希望我没有模糊不清:)

2 个答案:

答案 0 :(得分:2)

我喜欢从vitsoft上面编写上面代码的一些变体,而不使用软件中断将字符串直接打印到我们必须在下面的代码中指定的给定屏幕坐标。 (但它不会触摸或移动光标位置。)对于64位Windows,请使用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

答案 1 :(得分:-2)

如果DOS功能不足裸机,您可以在PC的固件中使用硬连线的BIOS功能。它们在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
相关问题