使用Assembly在屏幕上打印字符

时间:2012-01-12 16:33:55

标签: linux assembly x86 nasm

我在ubuntu 11.10中使用NASM并使用它编写了一个程序,下面的代码会一次打印出一个字符。

    [BITS 16]       ;Tells the assembler that its a 16 bit code
    [ORG 0x7C00]    ;Origin, tell the assembler that where the code will

    ;segment .data
        PROMPT1 db  "HELLO WORLD",0x0
        STAR    db  '*'
    ;segment .text
       ;global asm_main
    ;asm_main:
        mov     si, PROMPT1
        mov     al, [si]
        ;mov ecx, 11
    loop_start:
       call    PRNTCHR
       inc     si
       mov al, 0xA
       call PRNTCHR
       mov     al, [si]
       cmp     al, 0
       je      $+4
       loop    loop_start
       jmp         $

   PRNTCHR:
       MOV AH, 0x0E        ;Tell BIOS that we need to print one charater on screen.
       MOV BH, 0x00        ;Page no.
       MOV BL,0x0F        ;Text attribute 0x07 is lightgrey font on black background

       INT 0x10    ;Call video interrupt
       RET         ;Return to calling procedure

   TIMES 510 - ($ - $$) db 0   ;Fill the rest of sector with 0
   DW 0xAA55                   ;Add boot signature at the end of bootloader

我对这段代码有两个问题。

  1. 虽然明星db'*'在代码中没有做任何事情但它很重要。
  2. 打印新行不能有效工作。程序也输出空格
  3. 你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

即使它是给定一个标识符,它仍然存在于二进制文件中,在加载到原点之后,处理器继续增加IP并运行代码,当'*'打印时它的情况纯属巧合(当PROMPT1转换为指令时!)。当存在分段时,这不会发生,但是在此模式下,默认情况下不启用分段,当将分段代码移植到非分段代码中时,您应该将非代码数据填充到您想要运行的代码之后,这很简单,谢谢亚历克斯(因为你想运行)!

相关问题