string - 使用 db 在程序集 NASM 中声明字符串

标签 string assembly x86 nasm bootloader

我正在按照教程在汇编中编写 hello world 引导加载程序,并且我正在 x-86 机器上使用 NASM 汇编器。这是我正在使用的代码:

[BITS 16]   ;Tells the assembler that its a 16 bit code
[ORG 0x7C00]    ;Origin, tell the assembler that where the code will
            ;be in memory after it is been loaded

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString    ;Call print string procedure
JMP $       ;Infinite loop, hang it here.


PrintCharacter: ;Procedure to print character on screen
;Assume that ASCII value is in register AL
MOV AH, 0x0E    ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00    ;Page no.
MOV BL, 0x07    ;Text attribute 0x07 is lightgrey font on black background

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



PrintString:    ;Procedure to print string on screen
;Assume that string starting pointer is in register SI

next_character: ;Lable to fetch next character from string
MOV AL, [SI]    ;Get a byte from string and store in AL register
INC SI      ;Increment SI pointer
OR AL, AL   ;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character  ;Fetch next character from string
exit_function:  ;End label
RET     ;Return from procedure


;Data
HelloString db 'Hello World', 0 ;HelloWorld string ending with 0

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

我很难理解如何使用 db 命令将完整的“Hello World”字符串放入一个字节中。据我了解,db代表定义字节,它将所述字节直接放置在可执行文件中,但“Hello World”肯定比一个字节大。我在这里缺少什么?

最佳答案

伪指令dbdwdd和 friend can define multiple items

db 34h             ;Define byte 34h
db 34h, 12h        ;Define bytes 34h and 12h (i.e. word 1234h)

它们也接受字符常量

db 'H', 'e', 'l', 'l', 'o', 0

但是这种语法对于字符串来说很尴尬,所以下一个逻辑步骤是提供显式支持

db "Hello", 0         ;Equivalent of the above
<小时/>

附注一般prefer the user-level directives ,尽管对于 [BITS][ORG] 是不相关的。

关于string - 使用 db 在程序集 NASM 中声明字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647537/

相关文章:

复制字符串函数,复制的字符串不在函数本身中打印

swift - 为什么存储子字符串可能会导致 Swift 中的内存泄漏?

.net - 为什么 String::IsNullOrEmpty() 在 C++/CLI 中返回 false?

c# - 是否有可以从 c# 访问的移位和复制 cpu 指令?

c++ - 是否可以在 X86 处理器上自动加载和存储?

c++ - _mm_max_ss 在 clang 和 gcc 之间有不同的行为

c - 使用 strcpy 的 64 位缓冲区溢出

c - 在 C 中构建二维字符串数组的引用运算符

assembly - x86 操作码指令解码

memory - 如何在WinMobile6上启用ARMv6非对齐访问?