nasm-将值从4字节寄存器移到1字节地址空间 [英] Nasm - move value from a 4 byte register into an 1 byte address space

查看:91
本文介绍了nasm-将值从4字节寄存器移到1字节地址空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将存储在eax,ebx,ecx等中的4字节值移动到分配的空间少于4字节的地址空间时,nasm的行为如何?当我将存储在 var 中的1字节值移到4字节寄存器时,nasm分别表现如何?

赞:

  .bssvar resb 1.文本mov eax,2000000000mov [var],eaxxor ebx,ebxmov ebx,[var] 

[var] ebx 有什么值?又为什么呢?当用%d 调用 printf 时,结果为 2000000000 .但是怎么可能呢? var 只能保存1个字节.怎么可能从中接收到需要更大字节数的数字?

解决方案

与MASM不同,NASM不会跟踪变量的大小.(它实际上没有变量,只有标签.)

因此,从/到4字节寄存器的 mov 指令将简单地覆盖(或读取)从标签 var 开始的四个字节中的任何内容.

NASM不会阻止您编写错误的代码;汇编语言没有变量或类型;由您决定使用对您的内存布局有意义的说明.

要做一个狭窄的商店: mov [var],al

要做一个狭窄的负载(到一个完整的寄存器中): movzx ebx,字节[var]

How does nasm behave when I move the 4 byte value stored in eax, ebx, ecx etc. to an address space that has less than 4 bytes of space allocated? Respectively how does nasm behave when I move a 1 byte value stored in var to an 4 byte register?

Like:

.bss
var resb 1
.text
mov eax, 2000000000
mov [var], eax

xor ebx, ebx
mov ebx, [var]

What values would [var] and ebx have? And why? When calling printf with %d I get 2000000000 as a result. But how can this be? var can only save 1 byte. How is it possible that a number that requires a larger amount of bytes can be received from it?

解决方案

Unlike MASM, NASM doesn’t track the size of variables. (It doesn’t actually have variables, it just has labels.)

So the mov instruction from/to a 4-byte register will simply overwrite (or read) whatever is in the four bytes starting at the label var.

NASM doesn't stop you from writing buggy code; assembly language doesn't have variables or types; it's up to you to use instructions that make sense for your memory layout.

To do a narrow store: mov [var], al

To do a narrow load (into a full register): movzx ebx, byte [var]

这篇关于nasm-将值从4字节寄存器移到1字节地址空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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