2017-01-14 2 views
2

Я следую учебному пособию, чтобы написать приветственный загрузчик в сборке, и я использую ассемблер NASM для машины x-86. Это код, я использую:Использование db для объявления строки в сборке 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 

У меня есть некоторые трудности понимания того, как я могу поместить строку полноциклового «Hello World» в один байты с помощью команды дб. Как я понимаю, db обозначает define byte, и он помещает указанный байт непосредственно в исполняемый файл, но, безусловно, «Hello World» больше байта. Что мне здесь не хватает?

+3

Когда строка появляется в 'db', строка автоматически разбивается на каждый отдельный символ и сохраняется в последовательных байтах. 'HelloString db 'Hello World', 0' разбивается и обрабатывается как' HelloString db 'H', 'e', ​​'l', 'l', 'o', '', 'W', 'o', 'r', 'l', 'd', 0' –

ответ

4

инструкции псевдо db, dw, dd и друзей 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 

P.S. Обычно prefer the user-level directives, хотя для [BITS] и [ORG] не имеет значения.