当前位置:  开发笔记 > 编程语言 > 正文

使用db在程序集NASM中声明一个字符串

如何解决《使用db在程序集NASM中声明一个字符串》经验,为你挑选了1个好方法。

我正在按照教程在汇编中编写一个hello world bootloader,我正在使用NASM汇编程序来安装x-86机器.这是我正在使用的代码:

[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'大于一个字节.我在这里错过了什么?



1> Margaret Blo..:

伪指令db,dw,dd和朋友可以定义多个项目

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

PS通常更喜欢用户级指令,尽管是[BITS][ORG]无关.

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有