在内存中彼此相邻的 NASM 标签中打印两个字符串而不是第一个 [英] In NASM labels next to each other in memory are printing both strings instead of first one

查看:16
本文介绍了在内存中彼此相邻的 NASM 标签中打印两个字符串而不是第一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NASM 中编程时遇到问题.我正在学习如何纯粹用汇编语言开发操作系统,并从创建引导加载程序开始.

I am having an issue while programming in NASM. I am learning how to develop an OS purely in assembly and have started by creating a boot loader.

我目前的目标是打印Hello, World!"和再见!"使用 BIOS 中断 0x10.

My goal currently is to print "Hello, World!" and "Goodbye!" using the the BIOS interrupt 0x10.

在屏幕上打印值时我似乎遇到了这个问题.两个标签在内存中似乎彼此相邻,导致打印一个字符串以打印另一个字符串的内容.

The issue I seem to be having occurs while printing values on the screen. Two labels appear to be next to each other in memory causing printing one string to print the contents of the other string.

为什么 hlen 没有在第一个字符串的末尾停止循环?

Why isn't hlen stopping the loop at the end of the first string?

    [org 0x7c00]

    mov ah, 0x0e

    mov bx, HELLO_MSG
    mov cx, hlen                                                                                                                                              
    call print_string

    mov bx, GOODBYE_MSG
    mov cx, glen                                                                                                                                              
    call print_string

    jmp $

    %include "print_string.asm"


    HELLO_MSG db 'Hello, World!',0
    GOODBYE_MSG db 'Goodbye!',0


    hlen equ $ - HELLO_MSG
    glen equ $ - GOODBYE_MSG

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

错误:

  1. 打印两次再见信息

  1. Prints goodbye message twice

这是由于 HELLO_MSG 打印 Hello, World!Goodbye!.我相信这是因为 Hello_MSG 标签紧挨着内存中的 GOODBYE_MSG 标签

This is due to the HELLO_MSG printing Hello, World! and Goodbye!. I believe this occurs because the Hello_MSG label is right next to the GOODBYE_MSG label in memory

;;;print_string.asm
print_string:                   ;cx = string length                                                                                                                                                          
                                ;bX = string label - memory offset                                                                                                                       
                                ; -- if you want the data at a memory adress use [bx]                                                                                                                        
        mov al, [bx]
        int 0x10

        inc bx

        loop print_string

        ret

推荐答案

您对 hlen 的计算包括字符串 Goodbye! 因为它出现在 的定义之后GOODBYE_MSG.表达式$ - HELLO_MSG 是标签HELLO_MSG 和定义hlen 的行之间的字节数.这就是为什么您第一次调用 print_string 会打印两条消息.

Your computation of hlen includes the string Goodbye! because it comes after the defintion of GOODBYE_MSG. The expression $ - HELLO_MSG is the number of bytes between the label HELLO_MSG and the line where hlen is defined. That is why your first call to print_string prints both messages.

试试这个订单:

HELLO_MSG db 'Hello, World!',0
hlen equ $ - HELLO_MSG

GOODBYE_MSG db 'Goodbye!',0
glen equ $ - GOODBYE_MSG

请参阅$ 在 NASM 中究竟是如何工作的?了解更多详情, 以这个为例.

See How does $ work in NASM, exactly? for more details, including this as an example.

这篇关于在内存中彼此相邻的 NASM 标签中打印两个字符串而不是第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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