定义字符串时db和dw的区别 [英] Difference between db and dw when defining strings

查看:95
本文介绍了定义字符串时db和dw的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NASM汇编中,有dbdw伪指令来声明数据.NASM 手册提供了几个例子,但没有直接说明是什么他们之间的区别.我已经对它们两个都尝试了以下hello world"代码,结果发现没有区别.我怀疑distinct与内部数据格式有关,但我不知道如何检查.

In NASM assembly, there are db and dw pseudo instructions to declare data. NASM Manual provides a couple of examples but doesn't say directly what's the difference between them. I've tried the following "hello world" code with both of them, and it turned out that no difference is observable. I suspect the distinct has something to do with internal data format, but I don't know how to inspect that.

section .data
        msg db "hello world",10,13,0
        msg2 dw "hello world",10,13,0

section .text
global _start
_start:
        mov rax, 1
        mov rdi, 1
        mov rsi, msg ; or use msg2
        mov rdx, 14
        syscall
        jmp .exit

.exit:
        mov rax, 60
        mov rdi, 0
        syscall

推荐答案

NASM 手册部分 3.2.1 DB 和朋友:声明初始化数据3.4.2 字符串表示当单个字符串短于元素大小时存在差异.每个元素都用零字节填充到其原始大小.

The NASM manual sections 3.2.1 DB and Friends: Declaring Initialized Data and 3.4.2 Character Strings indicate that there is a difference when the individual strings are shorter than the element size. Each element is padded with zero bytes to its native size.

为确保数据中没有意外字符,请始终将 DB 用于 8 位字符串.DW 可能适用于 UTF-16,也可能不适用于 UTF-16,具体取决于机器字节顺序和代码中的任何假设.

To ensure that you do not have unintended characters in the data, always use DB for 8-bit strings. DW may or may not work for UTF-16 depending on the machine byte order and any assumptions in the code.

使用 DW 伪指令肯定会导致数值出现意外值,因为这些值将被解释为 16 位字,将意外空字符引入字符串中.

Using DW pseudo instructions will definitely result in unexpected values for the numeric values as these will be interpreted as 16-bit words introducing unexpected null characters into the string.

使用 2.1.3 -l 选项:生成列表文件 查看正在输出的实际内存映像以查看您正在生成的内容.

Use 2.1.3 The -l Option: Generating a Listing File to see the actual memory image being output to see the content you are generating.

这篇关于定义字符串时db和dw的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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