2017-07-14 4 views
0

文字列を印刷したい、NASMアセンブリを使用して、プログラムを実行するBochs、2つのシンプルなファイルがあります。私はアセンブリを学ぶのは非常に簡単なブートセクタを作っています。私は自分自身を教えようとしており、このPDFを使用しています: https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf 私自身の文字列印刷機能を作成しようとしています。NASM with Bochs - アセンブリ - 文字列の印刷

問題:

; Question 4 
; Put together all of the ideas in this section to make a self-contained function for printing 
; null-terminated strings, that can be used as follows: 
; 
; A boot sector that prints a string using our function. 
; 
[org 0x7c00] ; Tell the assembler where this code will be loaded 
mov bx, HELLO_MSG ; Use BX as a parameter to our function , so 
call print_string ; we can specify the address of a string. 
mov bx, GOODBYE_MSG 
call print_string 
jmp $ ; Hang 
%include "print_string.asm" 
; Data 
HELLO_MSG: 
db ’Hello , World!’, 0 ; <-- The zero on the end tells our routine 
; when to stop printing characters. 
GOODBYE_MSG: 
db ’Goodbye!’, 0 
; Padding and magic number. 
times 510-($-$$) db 0 
dw 0xaa55 
; For good marks, make sure the function is careful when modifying registers and that 
; you fully comment the code to demonstrate your understanding. 

マイコード:

bootsect.asm

org 0x7c00 ; Start at boot sector. Allows us to add offset for labels efficiently 


    mov bx, loading_sys_msg 
    calltest: 
    call str_out 
    ;jmp calltest 

    jmp $ ; Jump forever. Because end of program lol 

; Includes 
    %include "str_out.asm" 

; Database Resources 

    loading_sys_msg: 
    db 'Loading OIK v0.0.1', 0 

; Padding and magic BIOS number. 

times 510-($-$$) db 0 
dw 0xaa55 
` 

str_out.asm

; 
; String-printing function 
; 
str_out: 
pusha 
pop bx 
;add bx, 0x7c00 ; Adds current address if boot sect and no org called (not used) 
mov ah, 0x0e ; BIOS teletyping for 0x10 interrupt 
mov ax, 0x00 ;prep counter 
mov al, [bx] ; character to print placed in al (bx address contents) 
prnt:     ; printing loop 
int 0x10   ; interrupt print 
add ax, 0x01 ; add to counter for removal afterwards 
add bx, 0x01 ; move bx address forward by 1 
mov al, [bx] ; character to print placed in al (bx address contents) 
cmp al, 0   ; Compare [al -?- 0] 
jg prnt    ; If greater, jump to print 
sub bx, ax  ;remove the counter amount 
;sub bx, 0x7c00 ;remove address addition if used earlier (not used) 
push bx 
popa 

Bochsの設定:

# Tell bochs to use our boot sector code as though it were 
# a floppy disk inserted into a computer at boot time. 
floppya: 1_44=boot_sect.bin, status=inserted 
boot: a 

Bochsが起動すると、画面が消えて何も起こりません。私は間違って何をしていますか?

は(私は問題を述べていなかった私に告げるためジェスターのおかげで。私はまだスタックオーバーフローに新しいです。)

+2

あなたは問題が何か言及するのを忘れていました...また、bochsには内蔵のデバッガがあり、必ずそれを使用してください。 PS: 'pusha;ポップbxは悪い習慣であり、あなたが思っていることを実行していない可能性があります(もちろん、あなたがしたいことをコメントしませんでした) – Jester

+0

アドバイスをいただき、ありがとうございます。プサをするとき。 pop bx'私はすべてのvarsをスタックにプッシュし、bxだけを取り出しようとしました。私は代わりにbx以外のすべての瓶を押し込み、私が終わったらそれらを飛び出そうとします。 – C317SO

+2

'pop bx'はプッシュされたものから' bx'の元の値を検索しません、単にスタックから一番上の項目を削除します。 'pusha'の仕組みのせいで' bx'になることはありません。とにかく、 'pusha'は' bx'を変更しません。値はまだありますので、 'pop'する必要はありません。 – Jester

答えて

0

str_outは、RET命令が含まれていませんが、あなたはAH

をゴミ箱ので、ディスプレイの欠如があります
mov ah, 0x0e ; BIOS teletyping for 0x10 interrupt 
mov ax, 0x00 ;prep counter 

通常、私は演習に例を挙げませんが、この場合、あなたは妥当な努力をして、あなたのロジックを得ることができます。 が何を意味するか

  org  0x7c00 

      mov  bx, Msg 
      call str_out 
      hlt 
      jmp  $ - 1 

str_out: mov  ah, 0x0e 
prnt:  mov  al, [bx] 
      int  0x10 
      add  bx, 0x01 
      cmp  al, 0 
      jg  prnt 
      ret 

    Msg:  db  'Loading OIK v0.0.1', 0 

    times 510-($-$$) db 0 
     dw 0xaa55 

正確にわからないが、レジスタを変更するときに、関数は慎重になっていることを確認しますが、この例では、作業を行います。これは本質的にあなたのコードのカーボンコピーであるので、あなたがする必要があることはそれを文書化することだけですが、あなたが尋ねられるかもしれない2つの特質があります。なぜ命令を使用したのですか?その命令はなぜですか?

+0

返信いただきありがとうございます。私はあなたがゴミを出してどういう意味かはっきりしていませんが、AXってどういう意味ですか?あなたが理解していないレジスタを使用しないように注意していたことを意味していたと思います...)また、私はあなたがコード内の関数です。私はそれがフォーマットの違いのように、ある意味で(利便性/アクセシビリティとバイナリの両方で)優先的なものだと思います。助けてくれてありがとう。 – C317SO

+1

@Kohrokhoはい、ゴミ箱はAH = 14に設定し、次の命令では消去します。私は '%include'を使うことに同意しますが、何千ものコード行を持っていれば差別化されますので、書式スタイルをお勧めします。あなたがそれをどうしても、結果がエラーフリーのコードであり、それを維持するのが簡単なら、引数はありません。 –