2017-01-14 1 views
2

Ich befolge ein Tutorial zum Schreiben eines Hallo Welt Bootloader in Assembly und ich verwende den NASM Assembler für eine x-86-Maschine. Dies ist der Code ich verwende:Verwenden von db zum Deklarieren einer Zeichenfolge in Assembly 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 

Ich habe einige Schwierigkeiten zu verstehen, wie ich die kompletten ‚Hallo Welt‘ String in ein Byte mit dem db Befehl platzieren können. Wie ich es verstehe, steht db für definieren Byte und es setzt das genannte Byte direkt in der ausführbaren Datei, aber sicherlich 'Hello World' ist größer als ein Byte. Was fehlt mir hier?

+3

Wenn eine Zeichenfolge in einer 'db' erscheint, wird die Zeichenfolge automatisch in jedes einzelne Zeichen aufgeteilt und in aufeinanderfolgenden Bytes gespeichert. 'HalloString db 'Hallo Welt', 0 'ist aufgeteilt und behandelt als' HalloString db 'H', 'e', ​​'l', 'l', 'o', '', 'W', 'o', 'r', 'l', 'd', 0 ' –

Antwort

4

Die Pseudobefehle db, dw, dd und Freunde can define multiple items

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

Sie akzeptieren zu Zeichenkonstanten

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

aber diese Syntax ist für Streicher ungeschickt, so war der nächste logische Schritt zu geben, explizite Unterstützung

db "Hello", 0   ;Equivalent of the above 

P.S. Im Allgemeinen ist prefer the user-level directives, obwohl für und [ORG] irrelevant.

Verwandte Themen