内存中彼此相邻的标签导致打印问题

内存中彼此相邻的标签导致打印问题

本文介绍了在NASM中,内存中彼此相邻的标签导致打印问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NASM中编程时遇到问题.我正在学习如何纯粹以组装方式开发OS,并且已经从创建引导加载程序开始了.

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中,内存中彼此相邻的标签导致打印问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:53