assembly - hello world

the program find max number from a list of numbers, using assembly language on x86 architecture,

 

code:

    max_num2.s:

 

# find max number in a list of numbers, loop reverse,
# and don't need the register that record total_count,
# 
# registers:
#	%eax	store each number to compare
#	%ebx	the max number
#	%edi	index of number during comparation

.section .data
num_items:
	.long 3,67,34,222,45,75,54,34,44,33,22,11,66
num_count:
	.long 13

.section .text
.globl _start

_start:
movl num_items, %ebx
movl num_count, %edi
decl %edi

loop:
cmp $0, %edi			# detect whether reach end
jl exit
movl num_items(,%edi,4), %eax	# read next number
decl %edi			# decrease index by 1
cmp %eax, %ebx
jge loop
movl %eax, %ebx
jmp loop

exit:
movl $1, %eax
int $0x80

 

compile:

    as max_num2.s -o a.o

    ld a.o -o a.out

 

run:

    ./a.out

 

get result: (the result is stored in %ebx, then call exit syscall, which read status code from this register)

    echo $?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值