不使用predefined功能组件打印字符串 [英] Printing a string in assembly using no predefined function

查看:156
本文介绍了不使用predefined功能组件打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须定义在汇编函数,让我遍历宣布字节的字符串,并使用BIOS中断打印。我在16位实模式。这是从一本教科书编写一个小的bootloader练习,但似乎这只是一个草案,它缺少一些东西。
我一直在考虑以下code:

I have to define a function in assembly that will allow me to loop through a string of declared bytes and print them using a BIOS interrupt. I'm in 16 bit real mode. This is an exercise on writing a little bootloader from a textbook, but it seems that it was only a draft and it's missing some stuff. I have been given the following code:

org 0x7c00

mov bx, HELLO_MSG
call print_string

mov bx, GOODBYE_MSG
call print_string

jmp $                ;hang so we can see the message

%include "print_string.asm"

HELLO_MSG:
    db 'Hello, World!', 0

GOODBYE_MSG:
    db 'Goodbye!', 0

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

我print_string.asm看起来是这样的:

My print_string.asm looks like this:

print_string:
    pusha
    mov ah, 0x0e

    loop:
        mov al, bl
        cmp al, 0
        je return
        int 0x10
        inc bx
        jmp loop

    return:
        popa
        ret

我有我在做什么了,但书中并没有解释如何通过一些迭代。我知道该怎么做,在C,但这个是用汇编比调试C code以外的东西我的第一次。什么时候我通过仿真器启动的情况是,它打印出一对夫妇乱码线,最终挂在那儿,我看到我的失败在它所有的荣耀。哈哈哈。

I have some idea of what I'm doing, but the book doesn't explain how to iterate through something. I know how to do it in C but this is my first time using assembly for something other than debugging C code. What happens when I boot through the emulator is that it prints out a couple of lines of gibberish and eventually hangs there for me to see my failure in all it's glory. Hahaha.

推荐答案

好了,它看起来像它加载字符串到BX地址调用之前注册。

Well, it looks like it loads the address of the string into the BX register before calling the function.

实际的功能看起来是通过串试图循环使用BX作为指针和递增它(INC BX),直到它击中ASCII NUL在字符串的结尾(CMP人,0; JE回报) ...

The actual function looks like it is trying to loop through the string, using BX as a pointer and incrementing it (inc bx) until it hits the ASCII NUL at the end of the string (cmp al, 0; je return)...

...但什么是错的。在MOV AL,BL指令不完全正确的,因为这将地址的低8位进入人一个ASCII NUL,不使有很大的意义进行比较。我想它应该是更喜欢MOV人,[BX];即移动的BX引用的字节地址到AL寄存器 - 尽管它已经很长一段时间,因为我已经与装配工作,所以我可能没有语法正确

...but something is wrong. The "mov al, bl" instruction does not look correct, because that would move the low 8 bits of the address into al to be compared for an ASCII NUL, which does not make a lot of sense. I think it should be something more like "mov al, [bx]"; i.e. move the byte referenced by the BX address into the AL register -- although it has been a long time since I've worked with assembly so I might not have the syntax correct.

由于该错误的,在10小时的中断也将基于字符串而不是字符串的内容的地址打印随机字符。这可以解释你所看到的胡言乱语。

Because of that bug, the 10h interrupt would also be printing random characters based on the address of the string rather than the contents of the string. That would explain the gibberish you're seeing.

这篇关于不使用predefined功能组件打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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