close

.section .data
output: .asciz "總和: %d\n"
msg: .asciz "降冪加總( n + ... + 1 ), n= "
ifmt: .asciz "%d"
n: .int 0
.section .text
.globl _main
_main:
pushl $msg
call _printf
addl $4, %esp
pushl $n # 輸入整數存入 n
pushl $ifmt # 即 scanf("%d", &n);
call _scanf
addl $8, %esp
movl n, %ecx
movl $0, %eax
loop1:
addl %ecx, %eax
loop loop1
pushl %eax
pushl $output
call _printf
add $8, %esp
pushl $0
call _exit






.section .data
msg: .asciz "請輸入1個奇數"
ifmt:.asciz "%d"
output: .asciz "1+3+5+...+%d=%d"
n: .int 0
sum: .int 0

.section .text
.globl _main
_main:
pushl $msg
call _printf
addl $4, %esp

pushl $n
pushl $ifmt
call _scanf
addl $8, %esp

movl $1, %edi
again:
addl %edi, sum
addl $2, %edi
cmp n, %edi
ja over
jmp again

over:
pushl sum
pushl n
pushl $output
call _printf
addl $12, %esp

pushl $0
call _exit














.section .data
n: .int 0
msg: .asciz "請輸入一個整數:"
ifmt: .asciz "%d"
up: .asciz "%d是一個正數"
down: .asciz "%d是一個負數"

.section .text
.globl _main

_main:
pushl $msg
call _printf
addl $4, %esp

pushl $n
pushl $ifmt
call _scanf
addl $8, %esp

movl $0, %ebx
pushl n
cmp %ebx, n
jge big
pushl $down
jmp out

big:
pushl $up

out:
call _printf
addl $8, %esp

pushl $0
call _exit








#輸入年齡,印出全票或半票

.section .data
n: .int 0
msg: .asciz "請輸入年齡:"
ifmt: .asciz "%d"
all: .asciz "全票"
half: .asciz "半票"
fault: .asciz "年齡必須大於0,請重新輸入!"

.section .text
.globl _main

_main:
pushl $msg
call _printf
addl $4, %esp

again:
pushl $n
pushl $ifmt
call _scanf
addl $8, %esp

cmp $0, n
jle bug

cmp $65, n
jg else
cmp $18, n
jl else

then:
pushl $all
jmp end_if

bug:
pushl $fault
call _printf
addl $4, %esp
jmp again

else:
pushl $half

end_if:
call _printf
addl $8, %esp

pushl $0
call _exit














#輸入月份,判斷季節

.section .data
month: .int 0
msg: .asciz"輸入月份!"
ifmt: .asciz"%d"
spring: .asciz"春"
summer: .asciz"夏"
fall: .asciz"秋"
winter: .asciz"冬"
fault: .asciz"沒有這個月份!"

.section .text
.globl _main

_main:
pushl $msg
call _printf
addl $4, %esp

again:
pushl $month
pushl $ifmt
call _scanf
addl $8, %esp

cmp $0, month
jle bug
cmp $12, month
jg bug

movl $2, %edi

cmp %edi, month
jle wt
cmp $5, month
jle sp
cmp $8, month
jle sm
cmp $11, month
jg wt
pushl $fall
jmp end_if

sm:
pushl $summer
jmp end_if

bug:
pushl month
pushl $fault
call _printf
addl $8, %esp
jmp again

sp:
pushl $spring
jmp end_if

wt:
pushl $winter

end_if:

call _printf
addl $4, %esp

pushl $0
call _exit












#判段座標所在象限

.section .data
x: .int 0
y: .int 0
msg: .asciz "請輸入兩個整數x和y,放進座標(x,y)"
ifmt: .asciz "%d %d"
one: .asciz "座標(%d,%d)在第一象限"
two: .asciz "座標(%d,%d)在第二象限"
three: .asciz "座標(%d,%d)在第三象限"
four: .asciz "座標(%d,%d)在第四象限"
zero: .asciz "座標(%d,%d)在原點上"
onx: .asciz "座標(%d,%d)在X軸上"
ony: .asciz "座標(%d,%d)在Y軸上"

.section .text
.globl _main

_main:
pushl $msg
call _printf
addl $4, %esp

pushl $y
pushl $x
pushl $ifmt
call _scanf
addl $12, %esp

pushl y
pushl x
cmp $0, x
je x_0
cmp $0, y
je y_0

cmp $0, x
jg right
cmp $0, y
jg l_up
pushl $three
jmp end_if


right:
cmp $0, y
jg r_up
pushl $four
jmp end_if

r_up:
pushl $one
jmp end_if

l_up:
pushl $two

x_0:
cmp $0, y
je start
pushl $ony
jmp end_if

y_0:
pushl $onx
jmp end_if

start:
pushl $zero

end_if:
call _printf
addl $12, %esp

push $0
call _exit




























全站熱搜
創作者介紹
創作者 alex2008 的頭像
alex2008

alex2008的部落格

alex2008 發表在 痞客邦 留言(0) 人氣()