没有合适的资源?快使用搜索试试~ 我知道了~
Linux assemblers: A comparison of GAS and NASM
需积分: 3 0 下载量 17 浏览量
2018-08-22
10:16:51
上传
评论
收藏 282KB PDF 举报
温馨提示
试读
15页
Linux assemblers: A comparison of GAS and NASM A side-by-side look at GNU Assembler (GAS) and Netwide Assembler (NASM).
资源推荐
资源详情
资源评论
© Copyright IBM Corporation 2007 Trademarks
Linux assemblers: A comparison of GAS and NASM Page 1 of 15
Linux assemblers: A comparison of GAS and NASM
A side-by-side look at GNU Assembler (GAS) and Netwide
Assembler (NASM)
Ram Narayan October 17, 2007
This article explains some of the more important syntactic and semantic differences between
two of the most popular assemblers for Linux®, GNU Assembler (GAS) and Netwide Assembler
(NASM), including differences in basic syntax, variables and memory access, macro handling,
functions and external routines, stack handling, and techniques for easily repeating blocks of
code.
Introduction
Unlike other languages, assembly programming involves understanding the processor
architecture of the machine that is being programmed. Assembly programs are not at all portable
and are often cumbersome to maintain and understand, and can often contain a large number of
lines of code. But with these limitations comes the advantage of speed and size of the runtime
binary that executes on that machine.
Though much information is already available on assembly level programming on Linux, this article
aims to more specifically show the differences between syntaxes in a way that will help you more
easily convert from one flavor of assembly to the another. The article evolved from my own quest
to improve at this conversion.
This article uses a series of program examples. Each program illustrates some feature and is
followed by a discussion and comparison of the syntaxes. Although it's not possible to cover every
difference that exists between NASM and GAS, I do try to cover the main points and provide a
foundation for further investigation. And for those already familiar with both NASM and GAS, you
might still find something useful here, such as macros.
This article assumes you have at least a basic understanding of assembly terminology and have
programmed with an assembler using Intel® syntax, perhaps using NASM on Linux or Windows.
This article does not teach how to type code into an editor or how to assemble and link (but see
the sidebar for a quick refresher). You should be familiar with the Linux operating system (any
developerWorks® ibm.com/developerWorks/
Linux assemblers: A comparison of GAS and NASM Page 2 of 15
Linux distribution will do; I used Red Hat and Slackware) and basic GNU tools such as gcc and ld,
and you should be programming on an x86 machine.
Now I'll describe what this article does and does not cover.
Building the examples
Assembling:
GAS:
as –o program.o program.s
NASM:
nasm –f elf –o program.o program.asm
Linking (common to both kinds of assembler):
ld –o program program.o
Linking when an external C library is to be used:
ld –-dynamic-linker /lib/ld-linux.so.2 –lc –o program program.o
This article covers:
• Basic syntactical differences between NASM and GAS
• Common assembly level constructs such as variables, loops, labels, and macros
• A bit about calling external C routines and using functions
• Assembly mnemonic differences and usage
• Memory addressing methods
This article does not cover:
• The processor instruction set
• Various forms of macros and other constructs particular to an assembler
• Assembler directives peculiar to either NASM or GAS
• Features that are not commonly used or are found only in one assembler but not in the other
For more information, refer to the official assembler manuals (see Related topics for links), as
those are the most complete sources of information.
Basic structure
Listing 1 shows a very simple program that simply exits with an exit code of 2. This little program
describes the basic structure of an assembly program for both GAS and NASM.
Listing 1. A program that exits with an exit code of 2
Line NASM GAS
ibm.com/developerWorks/ developerWorks®
Linux assemblers: A comparison of GAS and NASM Page 3 of 15
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
; Text segment begins
section .text
global _start
; Program entry point
_start:
; Put the code number for system call
mov eax, 1
; Return value
mov ebx, 2
; Call the OS
int 80h
# Text segment begins
.section .text
.globl _start
# Program entry point
_start:
# Put the code number for system call
movl $1, %eax
/* Return value */
movl $2, %ebx
# Call the OS
int $0x80
Now for a bit of explanation.
One of the biggest differences between NASM and GAS is the syntax. GAS uses the AT&T syntax,
a relatively archaic syntax that is specific to GAS and some older assemblers, whereas NASM
uses the Intel syntax, supported by a majority of assemblers such as TASM and MASM. (Modern
versions of GAS do support a directive called .intel_syntax, which allows the use of Intel syntax
with GAS.)
The following are some of the major differences summarized from the GAS manual:
• AT&T and Intel syntax use the opposite order for source and destination operands. For
example:
• Intel: mov eax, 4
• AT&T: movl $4, %eax
• In AT&T syntax, immediate operands are preceded by $; in Intel syntax, immediate operands
are not. For example:
• Intel: push 4
• AT&T: pushl $4
• In AT&T syntax, register operands are preceded by %; in Intel syntax, they are not.
• In AT&T syntax, the size of memory operands is determined from the last character of the
opcode name. Opcode suffixes of b, w, and l specify byte (8-bit), word (16-bit), and long (32-
bit) memory references. Intel syntax accomplishes this by prefixing memory operands (not the
opcodes themselves) with byte ptr, word ptr, and dword ptr. Thus:
• Intel: mov al, byte ptr foo
• AT&T: movb foo, %al
• Immediate form long jumps and calls are lcall/ljmp $section, $offset in AT&T syntax; the
Intel syntax is call/jmp far section:offset. The far return instruction is lret $stack-adjust
in AT&T syntax, whereas Intel uses ret far stack-adjust.
In both the assemblers, the names of registers remain the same, but the syntax for using them is
different as is the syntax for addressing modes. In addition, assembler directives in GAS begin with
a ".", but not in NASM.
The .text section is where the processor begins code execution. The global (also .globl or
.global in GAS) keyword is used to make a symbol visible to the linker and available to other
剩余14页未读,继续阅读
资源评论
pngliu
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功