AT&T Assembly Language Samples

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






阅读更多
上一篇读取PBOC电子现金指令流
下一篇请尽量不要为全局变量赋不必要的初值!
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭