在 Mac 上使用汇编
我使用的是配备 2.53 GHz Intel Core 2 Duo 处理器的 MacBook Pro,但我被告知 Mac 用户必须遵循 AT&T 语法(这让我更加困惑,因为我运行的是 Intel)和 x86(不确定这意味着什么)确切地)。
所以我需要开始组装,但我发现很难开始。在线搜索显示汇编代码的语法差异很大,我找不到任何解释基本汇编方法的资源。我一直在阅读有关寄存器和堆栈的内容,但不明白如何看待它。谁能解释/指出我正确的方向?举个例子,这段代码是我发现的唯一有效的代码:
.data
_mystring: .ascii "Hello World\n\0" #C expects strings to terminate with a 0.
.text
.globl _foo
_foo:
push %ebp
mov %esp,%ebp
pushl $_mystring
call _myprint
add $4,%esp
pop %ebp
ret
非常简单,但它在说什么?我对这段代码如何执行其功能感到困惑。我了解 Java、PHP 和 C 等语言,但它的步骤和语法对我来说并不清楚。这是与之配套的主文件:
#include <stdio.h>
void foo();
void myprint(char *s)
{printf("%s", s);}
main()
{foo();}
另外,还有一个只是将数字相乘的文件:
.data
.globl _cntr
_cntr: .long 0
.globl _prod
_prod: .long 0
.globl _x
_x: .long 0
.globl _y
_y: .long 0
.globl _mask
_mask: .long 1
.globl _multiply
multiply:
push %ebp
mov %ebp,%esp
mov $0,%eax
mov _x,%ebx
mov _y,%edx
LOOP:
cmp $0,%ebx
je DONE
mov %ebx,%ecx
and $1,%ecx
cmp $1,%ecx
jne LOOPC
add %edx,%eax
LOOPC:
shr $1,%ebx
shl $1,%edx
jmp LOOP
DONE:
pop %ebp
ret
以及与之配套的 main.c:
#include <stdio.h>
extern int multiply();
extern int x, y;
int main()
{
x = 34;
y = 47;
printf("%d * %d = %d\n", x, y, multiply());
}
最后是三个小问题:
.s
和.h
文件名有什么区别(我有main.c
和>main.h
,哪个是做什么的)?为什么程序集需要一个
main.c
来配合它/它如何调用它?谁能推荐一个好的汇编 IDE,比如适用于 Java 或 PHP 的 Eclipse
感谢任何人的回答(这实际上是我在该网站上的第一篇文章) ,几天来我一直在试图解决这个问题,但我读过的所有资源都没有向我解释汇编逻辑。它说明了 .data
或 .text
的作用,但只有知道如何“思考汇编”的人才能理解它们的含义? 此外,如果有人在纽约市附近并且对 Assembly 和 CI 感到非常满意,那么他会喜欢一些私人课程。我觉得这门语言有很大的潜力,并且很想学习它。
I'm using a MacBook Pro with an Intel Core 2 Duo processor at 2.53 GHz, but I was told Mac users must follow AT&T syntax (which adds to my confusion since I am running Intel) and x86 (not sure what this means exactly).
So I need to get into assembly but am finding it very hard to even begin. Searches online show assembly code that varies greatly in syntax and I can't find any resources that explain basic assembly how-tos. I keep reading about registers and a stack but don't understand how to look at this. Can anyone explain/point me in the right direction? Take, for example, this code which is the only code I found to work:
.data
_mystring: .ascii "Hello World\n\0" #C expects strings to terminate with a 0.
.text
.globl _foo
_foo:
push %ebp
mov %esp,%ebp
pushl $_mystring
call _myprint
add $4,%esp
pop %ebp
ret
Very simple but what is it saying? I am having a confusing time understanding how this code does what it does. I know Java, PHP, and C, among other languages, but this, the steps and syntax of it, isn't clear to me. Here's the main file to go with it:
#include <stdio.h>
void foo();
void myprint(char *s)
{printf("%s", s);}
main()
{foo();}
Also, there's this which just multiplies numbers:
.data
.globl _cntr
_cntr: .long 0
.globl _prod
_prod: .long 0
.globl _x
_x: .long 0
.globl _y
_y: .long 0
.globl _mask
_mask: .long 1
.globl _multiply
multiply:
push %ebp
mov %ebp,%esp
mov $0,%eax
mov _x,%ebx
mov _y,%edx
LOOP:
cmp $0,%ebx
je DONE
mov %ebx,%ecx
and $1,%ecx
cmp $1,%ecx
jne LOOPC
add %edx,%eax
LOOPC:
shr $1,%ebx
shl $1,%edx
jmp LOOP
DONE:
pop %ebp
ret
and the main.c to go with it:
#include <stdio.h>
extern int multiply();
extern int x, y;
int main()
{
x = 34;
y = 47;
printf("%d * %d = %d\n", x, y, multiply());
}
And finally three small questions:
What is the difference between
.s
and.h
file names (I have both amain.c
andmain.h
, which one is for what)?And why does assembly need a
main.c
to go with it/how does it call it?Can anyone recommend a good assembly IDE like Eclipse is for Java or PHP
Thanks to whomever answers (this is actually my first post on this site), I've been trying to figure this out for a few days and every resource I have read just doesn't explain the assembly logic to me. It says what .data
or .text
does but only someone who knows how to "think assembly" would understand what they mean?
Also, if anyone is around New York City and feels very comfortable with Assembly and C I would love some private lessons. I feel there is a lot of potential with this language and would love to learn it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
汇编语言是一类与CPU架构密切相关的编程语言。传统上,每条汇编指令和生成的 CPU 指令之间存在一一对应的关系。
还有一些汇编伪指令,它们不对应于CPU指令,而是影响汇编器或生成的代码。
.data
和.text
是伪指令。从历史上看,每个 CPU 制造商都实现了由其汇编器(一种源代码翻译实用程序)定义的汇编语言。已经定义了数千种特定的汇编语言。
现代,人们已经认识到每种汇编语言都有许多共同的特征,特别是在伪指令方面。 GNU 编译器集合 (GCC) 基本上支持所有 CPU 架构,因此它已经发展了通用汇编功能。
x86
指 Intel 8086 系列(8088、8086、8087、80186、80286、80386、80486、80586 又名 Pentium、80686 又名 Pentium II 等)AT&T 语法 是许多汇编语言使用的表示法样式架构。一个主要特征是指令操作数按照从,到的顺序写入,这在历史上是常见的。 英特尔语法使用to、from操作数。还有其他差异。
对于您的许多问题,这里有一些资源,这些资源将 1) 让您不知所措,2) 最终为您提供所有答案:
全面的 ,汇编语言编程入门课程是一个完整的学期,有大量的实践工作。它假设您熟悉计算机体系结构的基础知识。合理预期理解上述材料将需要 300-500 小时。祝你好运!
Assembly language is a category of programming languages which are closely tied to CPU architectures. Traditionally, there is a one-to-one correspondence between each assembly instruction and the resulting CPU instruction.
There are also assembly pseudo-instructions which do not correspond to CPU instruction, but instead affect the assembler or the generated code.
.data
and.text
are pseudo-instructions.Historically, each CPU manufacturer implemented an assembly language as defined by their assembler, a source code translation utility. There have been thousands of specific assembly languages defined.
In modern times, it has been recognized that each assembly language shares a lot of common features, particularly with respect to pseudo-instructions. The GNU compiler collection (GCC) supports essentially every CPU architecture, so it has evolved generic assembly features.
x86
refers to the Intel 8086 family (8088, 8086, 8087, 80186, 80286, 80386, 80486, 80586 aka Pentium, 80686 aka Pentium II, etc.)AT&T syntax is a notation style used by many assembly language architectures. A major feature is that instruction operands are written in the order from, to as was common historically. Intel syntax uses to, from operands. There are other differences as well.
As for your many questions, here are some resources which will 1) overwhelm you, and 2) eventually provide all your answers:
Ordinarily, an introductory assembly language programming class is a full semester with plenty of hands-on work. It assumes you are familiar with the basics of computer architecture. It is reasonable to expect that understanding the above material will take 300-500 hours. Good luck!