汇编代码跳过一行? [英] Assembly code skipping a line?

查看:319
本文介绍了汇编代码跳过一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的汇编代码跳过一行?它一直跳过mov AX,A

Why is my assembly code skipping a line? It keeps skipping the line mov AX,A

org 100h
count equ 2
A DW 5
B DW 6
Y0 DW ?
Y1 DW ?    
mov AX,A     
add AX,B
sub AX,count
mov Y0,AX
mov BX,B
neg BX
add BX,count
mov Y1,BX 
ret

推荐答案

让我们看看这些指令助记符将组装成哪些 code字节:

Let's see what code bytes these instruction mnemonics would assemble to:

               org 100h
               count equ 2
05 00          A DW 5
06 00          B DW 6
00 00          Y0 DW ?
00 00          Y1 DW ?    
A1 00 01       mov AX,A       ;NASM "mov ax, [A]"
03 06 02 01    add AX,B       ;NASM "add ax, [B]"
83 E8 02       sub AX,count
...

代码字节在左边;指令助记符(提供给汇编器的部分)在右侧.一切看起来都不错吧?

The code bytes are on the left; the instruction mnemonics (the part you feed to the assembler) are on the right. All looks good, right?

好吧,现在让我们看看在汇编然后反汇编代码时会发生什么:

Well, now let's see what happens when we assemble and then disassemble the code:

05 00 06           add ax,0x600
00 00              add [bx+si],al
00 00              add [bx+si],al
00 B8 00 01        add [bx+si+0x100],bh
05 02 01           add ax,0x102
83 E8 02           sub ax,byte +0x2

等等……发生了什么?!字节(左侧)相同,但是指令助记符全都不同!他们和你最初写的不一样!

Wait…what happened?! The bytes (on the left) are the same, but the instruction mnemonics are all different! They're not the same ones you originally wrote!

发生的事情是,位于​​代码顶部的 data 声明已全部由汇编程序完好无损地转换为字节,然后反汇编程序将这些(数据)字节转换回了可执行代码.它没有意识到它们是数据,因此尝试将它们解释为要执行的指令.这不仅导致指令不正确,而且引入了 frameshift突变,使下游的一切混乱

What happened is, the data declarations that were at the top of your code were all dutifully translated into bytes by the assembler, but then the disassembler translated those (data) bytes back into executable code. It didn't realize they were data, so it tried to interpret them as instructions to be executed. This resulted not only in the incorrect instructions, but also introduced a frameshift mutation that messed up everything downstream.

因此,它实际上并不是在跳过"指令mov ax, a.据计算机所知,该指令甚至不再存在.它正在执行您在第二段代码中看到的指令.基本上,它正在执行废话.记住:在汇编程序中,所有内容都只是字节!

So it's not actually "skipping" the instruction mov ax, a. That instruction isn't even there anymore, as far as the computer can tell. It is executing the instructions that you see in the second block of code. Basically, it is executing nonsense. Remember: in assembler, everything is just bytes!

解决方案应该很明显:不要将数据声明与可执行代码混在一起.最好的办法是将数据向下移到所有可执行代码下方的底部,其中执行将永远不会执行:

The solution should be obvious: don't mix data declarations in with your executable code. The best thing to do would be to move the data down to the bottom, below all of your executable code, where execution will never get:

org 100h
count equ 2

mov AX,A     
add AX,B
sub AX,count
mov Y0,AX
mov BX,B
neg BX
add BX,count
mov Y1,BX 
ret

A DW 5
B DW 6
Y0 DW ?
Y1 DW ?  

引入空格有助于将内容分解为逻辑块,从而使代码更具可读性.请注意,我已经将ORG指令留在了顶部,因为它必须在其中.我也没有将EQU指令移到底部.可以将其保留在那里,因为它仅定义了一个汇编时间常数.从一开始就可以看到,它不会在生成的机器代码中转换为任何字节.

Introducing whitespace helps to break things up into logical chunks, making the code more readable. Notice that I've left the ORG directive at the top, because it needs to be there. I also didn't move the EQU directive to the bottom. It's okay to leave that there because it just defines an assemble-time constant. As you can see from the very beginning, it isn't translated into any bytes in the resulting machine code.

如果您出于某种原因不想移动数据,则必须插入一条指令以跳过数据:

If you didn't want to move the data for whatever reason, then you would have to insert an instruction to jump over it:

org 100h
count equ 2

jmp Begin

A DW 5
B DW 6
Y0 DW ?
Y1 DW ?    

Begin:
    mov AX,A     
    add AX,B
    sub AX,count
    mov Y0,AX
    mov BX,B
    neg BX
    add BX,count
    mov Y1,BX 
    ret

这篇关于汇编代码跳过一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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