GAS编译的第一个汇编小程序

在Linux里写了第一个汇编小程序,呵呵,输入两个数字,计算出结果并显示。
保存为example.s,
# gcc -o example example.s

.section	.rodata
prompt:
.string	"Please input an integer:"
format:
.string	"%d"
oform:
.string "%d + %d = %dn"
.text
.globl main
.type	main, @function
main:
pushl	%ebp
movl	%esp, %ebp
subl	$32, %esp
andl	$-16, %esp
movl	$0, %eax
subl	%eax, %esp	;Instructions above have nothing meaningful.

movl	$prompt, (%esp)
call	printf		;Prompt for the first integer.
movl	%esp, %eax
addl	$20, %eax
movl	%eax, 4(%esp)
movl	$format, (%esp)
call	scanf		;Accept the first input.
movl	$prompt, (%esp)
call	printf		;Prompt for the second.
movl	%esp, %eax
addl	$16, %eax
movl	%eax, 4(%esp)
movl	$format, (%esp)
call	scanf		;Accept the second.
movl	16(%esp), %eax
addl	20(%esp), %eax	;Compute the sum of two integers
movl	%eax, 12(%esp)	;sum
movl	16(%esp), %eax
movl	%eax, 8(%esp)	;num2
movl	20(%esp), %eax
movl	%eax, 4(%esp)	;num1
movl	$oform, (%esp)
call	printf		;printf("%d + %d = %dn", num1, num2, sum);
leave
ret

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.