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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值