程序集(x86):< label>除非有跳转指令,否则不会执行db'string',0 [英] Assembly (x86): <label> db 'string',0 does not get executed unless there's a jump instruction

查看:153
本文介绍了程序集(x86):< label>除非有跳转指令,否则不会执行db'string',0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用头撞墙,以试图理解为什么 以下程序集未正确转储'HELLO_WORLD'的内容.

I've been banging my head against the wall in an attempt to understand why the following assembly is not correctly dumping the contents of 'HELLO_WORLD'.

; Explicitly set 16-bit
[ BITS 16 ]
[ ORG 0x7C00 ]

; Create label for hello world string terminated by null.
HELLO_WORLD db 'hello world', 0

start:
    ; Move address of HELLO_WORLD into si
    mov SI, HELLO_WORLD
    call print_string

    ; Continue until the end of time
    jmp $

print_string:
    loop:
        ; Retrieve value stored in address at si
        mov al, [SI]
        mov ah, 0x0E
        cmp al, 0
        ; Finish execution after hitting null terminator
        je  return
        INT 0x10
        ; Increment contents of si (address)
        inc SI
        jmp loop

    return:
        ret

; boot loader length *must* be 512 bytes.
times 510-($-$$) db 0
dw 0xAA55

最后,我发现如果我们不执行标签(使其不成为代码),那么标签将正常运行.

In the end, I discovered that if we do not execute (make it not code) the label, then it functions correctly.

jmp start
HELLO_WORLD db 'hello world',0    

在十六进制转储中,我发现最令人困惑的部分是HELLO_WORLD仍在二进制文件中(开始时-它的类型似乎没有区别).

The part I find the most confusing, looking at the hex dump, HELLO_WORLD is still in the binary (at the beginning - and there appears to be no distinction of its type).

cat nojmp_boot.out

00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 00 be 00 7c e8  |hello world...|.|
00000010  02 00 eb fe 8a 04 b4 0e  3c 00 74 05 cd 10 46 eb  |........<.t...F.|
00000020  f3 c3 eb e8 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

cat jmpboot.out

00000000  eb 22 68 65 6c 6c 6f 20  77 6f 72 6c 64 00 be 02  |."hello world...|
00000010  7c e8 02 00 eb fe 8a 04  b4 0e 3c 00 74 05 cd 10  ||.........<.t...|
00000020  46 eb f3 c3 eb e8 00 00  00 00 00 00 00 00 00 00  |F...............|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

检查前两个字节,我们可以看到'e8 22'是对地址22的一次跳转(

Inspecting the first two bytes, we can see 'e8 22' is a shortjump to address 22 (http://net.cs.uni-bonn.de/fileadmin/user_upload/plohmann/x86_opcode_structure_and_instruction_overview.pdf).

我的问题是:

为什么我不能将"HELLO_WORLD"作为程序执行的一部分,据我所知,代码和数据之间没有区别?

Why can we not have 'HELLO_WORLD' as a part of the execution of the program, as far I was concerned, there was no distinction between code and data?

我正在使用以下内容进行编译:

I'm using the following for compilation:

nasm -f bin -o boot.bin boot.asm && if [ $(stat -c "%s" boot.bin) -ne 512 ]; then x; fi && qemu-system-x86_64 boot.bin

推荐答案

执行从顶部开始.如果省略jmp start,则字符 h 将被CPU解释为指令.您肯定会看到这样不正确吗?

Execution starts at the top. If you omit the jmp start then the character h will get interpreted by the CPU as if it were an instruction. Surely you see that such can not be correct?

就我而言,代码和数据之间没有区别吗?

as far I was concerned, there was no distinction between code and data?

当我们考虑二进制文件中的 placement 时,代码和数据之间没有区别.但是代码和数据仍然是2个完全不同的项目.代码是唯一可以由CPU 执行的代码.

There's no distinction between code and data when we consider their placement in the binary. But code and data still remain 2 completly different items. Code being the only one that can get executed by the CPU.

这篇关于程序集(x86):&lt; label&gt;除非有跳转指令,否则不会执行db'string',0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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