汇编-.data,.code和寄存器...? [英] Assembly - .data, .code, and registers...?

查看:383
本文介绍了汇编-.data,.code和寄存器...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,今天早上,我发布了一个关于汇编的困惑的问题,并且得到了一些真正的帮助,我非常感谢.

So this morning I posted a confused question about assembly and I received some great genuine help, which I really appreciate.

现在我开始组装并开始理解它的工作原理.

And now I'm starting to get into assembly and am beginning to understand how it works.

我认为我理解的东西包括堆栈,中断,二进制/十六进制,以及一般情况下大多数基本操作(jmp,push,mov等)的作用.

Things I feel I understand alright include the stack, interrupts, binary/hex, and in general what most of the basic operations do (jmp, push, mov, etc).

以下是我一直难以理解并希望获得帮助的概念-如果您可以解决以下任一问题,那将是巨大的帮助:

Concepts that I'm struggling to understand and would like help with are below - it would be a huge help if you could address any of the following:

  1. .data部分中到底发生了什么?是我们要声明的变量吗?
  2. 如果是这样,我们可以稍后在代码部分中声明变量吗?如果没有,为什么不呢?如果是这样,那么,为什么以及为什么要使用数据部分?
  3. 什么是寄存器?与变量相比如何?我的意思是我知道这是一个存储少量信息的位置...但是对我来说,这听起来像是一个变量.
  4. 如何制作数组?我知道这似乎是随机的,但是我对如何做这样的事情感到好奇.
  5. 是否有一些通用做法列表,列出了每个寄存器的用途?我仍然没有完全理解它们,但是已经注意到有人说,例如,应该使用某个寄存器来存储过程的返回值"-是否有此类实践的全面列表或至少提供了丰富信息? li>
  6. 我学习汇编语言的原因之一是为了更好地理解高级代码背后发生的事情.考虑到这一点-当我使用C ++进行编程时,我经常在考虑堆栈和堆.在组装过程中,我知道堆栈是什么-堆"在哪里?
  1. What exactly is happening in the .data section? Are those variables we're declaring?
  2. If so, can we declare variables later in the code section? If not, why not? If so, how, and why do we use the data section then?
  3. What's a register? How does it compare to a variable? I mean I know it's a location that stores a small piece of information... but that sounds exactly like a variable to me.
  4. How do I make an array? I know this seems kind of random, but I'm curious as to how I'd go about doing something like this.
  5. Is there a list somewhere of common practices for what each register should be used for? I still don't get them completely, but have noticed some people saying, for example, that a certain register should be used to store 'return values' from procedures - is there a comprehensive or at least informative list of such practices?
  6. One of the reasons I'm learning assembly is to better understand what's going on behind my high level code. With that in mind - when I'm programming in c++, I'm often thinking about the stack and the heap. In assembly I know what the stack is - where's the 'heap'?

一些信息:我将masm32与WinAsm一起用作IDE,并且正在Windows 7上工作.我有很多使用c ++/java等高级语言进行编程的经验.

Some info: I'm using masm32 with WinAsm as an IDE, and I'm working on Windows 7. I have a lot of prior experience programming in higher level languages such as c++/java.

edit:感谢大家的帮助,像往常一样非常有用!好东西!但是,最后一件事-我想知道堆栈指针与基本指针或ESP和EBP之间的区别是什么.有人可以帮我吗?

edit: Thanks for the help everyone, extremely informative as usual! Great stuff! One last thing though - I'm wondering what the difference is between the Stack Pointer, and the Base pointer, or ESP and EBP. Can someone help me out?

edit:我想我现在明白了……ESP始终指向栈顶.但是,您可以将EBP指向任何所需的位置. ESP是自动处理的,但是您可以使用EBP做任何您想做的事情.例如:

edit: I think I get it now... ESP always points to the top of the stack. However, you can point EBP at whatever you want. ESP is automatically handled but you can do whatever you want with EBP. For example:

push 6
push 5
push 4
mov EBP, ESP
push 3
push 2

在这种情况下,EBP现在指向地址为4的地址,但ESP现在指向地址为2的地址.

In this scenario, EBP now points to the address holding 4, but ESP now points to the address holding 2.

在实际的应用程序中,6、5和4可能是函数参数,而3和2可能是该函数中的局部变量.

In a real application 6, 5, and 4 could have been function arguments, whereas 3 and 2 could be local variables within that function.

推荐答案

让我们尝试按顺序回答!

Let's try to answer in order!

  1. 数据部分包含您要在调用程序的入口点之前由系统自动为您初始化的任何内容.没错,通常全局变量会在此处结束.零初始化数据通常不包含在可执行文件中,因为没有理由-生成该空间只需要几个指向程序加载器的指令即可.一旦程序开始运行,ZI和数据区域通常即可互换. 维基百科有很多信息.

在汇编程序设计中,变量实际上并不存在,至少在您编写C代码时并不如此.您所拥有的只是关于如何布置内存的决策.变量可以在堆栈上,也可以在内存中的某个地方,也可以仅存在于寄存器中.

Variables don't really exist when assembly programming, at least not in the sense they do when you're writing C code. All you have is the decisions you've made about how to lay out your memory. Variables can be on the stack, somewhere in memory, or just live only in registers.

寄存器是处理器的内部数据存储.通常,您只能对处理器寄存器中的值进行操作.您可以将它们的内容加载到内存中或从内存中存储,这是计算机工作方式的基本操作.这是一个简单的例子.此C代码:

Registers are the internal data storage of the processor. You can, in general, only do operations on values in processor registers. You can load and store their contents to and from memory, which is the basic operation of how your computer works. Here's a quick example. This C code:

int a = 5;
int b = 6;
int *d = (int *)0x12345678; // assume 0x12345678 is a valid memory pointer
*d = a + b;

可能会按照以下方式转换为某些(简化的)程序集:

Might get translated to some (simplified) assembly along the lines of:

load  r1, 5
load  r2, 6
load  r4, 0x1234568
add   r3, r1, r2
store r4, r3

在这种情况下,您可以将寄存器视为变量,但是一般而言,没有必要将任何一个变量始终保留在同一寄存器中.根据您的例程的复杂程度,它甚至可能无法实现.您需要将一些数据压入堆栈,弹出其他数据,依此类推. 变量"是逻辑数据,而不是它在内存或寄存器等中的存放位置.

In this case, you can think of the registers as variables, but in general it's not necessary that any one variable always stay in the same register; depending on how complicated your routine is, it may not even be possible. You'll need to push some data onto the stack, pop other data off, and so on. A 'variable' is that logical piece of data, not where it lives in memory or registers, etc.

数组只是一个连续的内存块-对于本地数组,您可以适当地递减堆栈指针.对于全局数组,可以在数据部分中声明该块.

An array is just a contiguous block of memory - for a local array, you can just decrement the stack pointer appropriately. For a global array, you can declare that block in the data section.

关于寄存器有很多约定-检查平台的ABI或调用约定文档以获取有关如何正确使用它们的详细信息.您的汇编器文档也可能包含信息.查看关于Wikipedia的ABI文章.

There are a bunch of conventions about registers - check your platform's ABI or calling convention document for details about how to use them correctly. Your assembler documentation might have information as well. Check the ABI article on wikipedia.

您的汇编程序可以使任何C程序都可以调用相同的系统,因此您只需调用malloc()即可从堆中获取内存.

Your assembly program can make the same system calls any C program could, so you can just call malloc() to get memory from the heap.

这篇关于汇编-.data,.code和寄存器...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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