AT&T Assembly Language Samples

原创 2015年07月07日 13:44:04

1. Simple Samples

1.1 cpuid.s

#cpuid.s Sample program to extract the processor Vendor ID
# - cpuid instruction code is used to gather information about the process,
#   depending on the value of the EAX register, the cpuid instruction will
#   produce different information about the process in the EBX, EDX, and ECX.
# - this program uses a Linux system call(int $0x80) to access the console 
#   display from the Linux kernel. The Linux kernel provides many preset 
#   functions. To access these kernel functions, you must use the int instruction
#   code, which generates a software interrupt, with a value of 0x80. The specific
#   function that is performed is determined by the value of the EAX register. 

.global _start

.section .data
# the x's are used as placeholders in the memory area reserved for the data variable.
output:
	.ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n"
	
.section .text
_start:
	# the zero value in EAX defines the CPUID output option.
	movl	$0,			%eax
	cpuid
	
	# the numbers outside the parentheses represent the location
	# relative to the output label where the data is placed.
	movl	$output,	%edi
	movl	%ebx,		28(%edi)	# low 4 bytes of the string
	movl	%edx,		32(%edi)	# middle 4 bytes of the string
	movl	%ecx,		36(%edi)	# last 4 bytes of the string
	
	# Linux write system call
	movl	$4,			%eax		# EAX contains the system call value
	movl	$1,			%ebx		# EBX contains the file descriptor to write to
	movl	$output,	%ecx		# ECX contains the start of the string
	movl	$42,		%edx		# EDX contains the length of the string
	int		$0x80
	
	# Linux exit system call
	movl	$1,			%eax		# system call 1(the exit function)
	movl	$0,			%ebx		# exit code value
	int		$0x80
result:
 

2. Moving Data

2.1 indexed_memory.s

# indexed_memory.s - example of using indexed memory locations

.global _start

.section .data
output:
	.asciz "The value is %d\n"
values:
	.int 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50

.section .text
_start:
	nop
	movl	$0,		%edi

loop:
	movl	values(, %edi, 4),	%eax
	pushl	%eax
	pushl	$output
	call printf
	addl	$8, %esp	# clear parameters placed on the stack
						# for the printf function
	inc		%edi
	cmpl	$11,	%edi
	jne		loop

	movl	$1,		%eax
	movl	$0,		%ebx
	int		$0x80
result:
$ as -o indexed_memory.o indexed_memory.s
$ ld -dynamic-linker /lib/ld-linux.so.2 -lc -o indexed_memory indexed_memory.o
$ ./indexed_memory
 
 
Knowledge: 1.indexed memory locations p101, 2.cleaning out the stack p312

2.2 cmovtest.s

# cmovtest.s - An example of the CMOV instructions
# finds the largest integer in a series defined in the values array

.global _start

.section .data
output:
	.asciz "The largest vlaue is %d\n"
values:
	.int 105, 235, 61, 315, 134, 221, 53, 145, 117, 5

_start:
	nop
	movl 	values, %eax
	movl	$1,		%edi
loop:
	movl	values(, %edi, 4), %ebx
	cmp		%ebx,	%eax
	cmovb	%ebx,	%eax		# condition move
	inc		%edi
	cmp		$10,	%edi
	jne		loop
	pushl	%eax
	pushl	$output
	call	printf			# call c library function
	addl	$8,		%esp	# clear parameters placed on the stack for printf
	pushl	$0
	call	exit






相关文章推荐

AT&T汇编-Brennan's Guide to Inline Assembly

DJGPP is a complete 32-bit C/C++ development system for Intel 80386 (and higher) PCs running DOS. It...

Assembly Intro - Intel and AT&T Syntax

 Intel and AT&T Syntax. Intel and AT&T syntax Assembly language are very diffe...

System call using assembly language and C

使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 选择一个系统调用(13号系统调用time除外),系统调用列表参见http://codelab.shiyanlou.com/xref/l...

Assembly Language Homework Project (Two)

Assembly Language Homework Project (Two) 键盘输入两个十进制非符号数(≤65535),计算两数之乘积,分别以十进制、十六进制、二进制输出结果。例如: ...
  • vamos12
  • vamos12
  • 2017年05月08日 23:51
  • 53

Interfacing DJGPP with Assembly-Language Procedures

Interfacing DJGPP with Assembly-Language Procedures    Written by Matthew Mastracci     Last Revi...
  • wbcuc
  • wbcuc
  • 2012年03月16日 15:29
  • 655

Assembly instructions 汇编指令解析 A&AT

Assembly instructions 汇编初步学习 汇编0基础的戳上面,刷一遍,包治百病 %eax寄存器的值与C语言的return的...

X86 Assembly Language and C Fundamentals

  • 2017年11月11日 22:25
  • 3.69MB
  • 下载

GNU as汇编简介 (AT&T语法)

一、简介 作为最基本的编程语言之一,汇编语言虽然应用的范围不算很广,但重要性却勿庸置疑,因为它能够完成许多其它语言所无法完成的功能。就拿 Linux 内核来讲,虽然绝大部分代码是用 C 语言编...

ARM A32 Assembly Language azw3

  • 2017年09月21日 16:45
  • 7.52MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AT&T Assembly Language Samples
举报原因:
原因补充:

(最多只允许输入30个字)