使用db在程序集NASM中声明字符串 [英] Using db to declare a string in assembly NASM

查看:347
本文介绍了使用db在程序集NASM中声明字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am following a tutorial to write a hello world bootloader in assembly and I am using the NASM assembler for an x-86 machine. This is the code I am using :

[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代表 define byte ,它将所述字节直接放置在可执行文件中,但是'Hello World'当然大于一个字节.我在这里想念什么?

I have some difficulty understanding how I can place the complete 'Hello World ' string into one byte using the db command. As I understand it , db stands for define byte and it places the said byte directly in the executable , but surely 'Hello World' is larger than a byte. What am I missing here ?

推荐答案

伪指令dbdwdd和朋友他们也接受字符常量

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

但是此语法对于字符串来说很尴尬,因此下一步的逻辑步骤是提供显式支持

but this syntax is awkward for strings, so the next logical step was to give explicit support

db "Hello", 0         ;Equivalent of the above


P.S.通常,首选用户级指令,尽管对于[BITS][ORG]不相关的.


P.S. In general prefer the user-level directives, though for [BITS] and [ORG] is irrelevant.

这篇关于使用db在程序集NASM中声明字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆