Monday, May 25, 2009

Assembly: Pecah Nilai Uang

Disini akan diperlihatkan assembly untuk melakukan pemecahan uang dengan nilai pecahan sebesar 50000, 20000, 10000, 5000, 1000. Program akan meminta masukan kelipatan 1000.
berikut sintaxnya :

.data
msg1:.asciiz "Masukkan uang Anda : "
msg2:.asciiz "\nPecahan 50000 : "
msg3:.asciiz "\nPecahan 20000 : "
msg4:.asciiz "\nPecahan 10000 : "
msg5:.asciiz "\nPecahan 5000 : "
msg6:.asciiz "\nPecahan 1000 : "
msg7:.asciiz "\nCoba lagi(0/1): "

msg8:.asciiz "#### Selesai ####"
.text
.globl main
main:
li $v0, 4
la $a0,msg1
syscall
li $v0, 5

syscall

add $t0,$zero,$v0 #t0 = inputan user
li $t1,1000
div $t0,$t1
mfhi $t2
bne $t2,$zero,main #kembali minta input bila inputan bukan kelipatan 1000

li $t1,50000 #t1 = 50000
div $t0,$t1

mflo $t2 #t2 = t0 div t1
li $v0, 4
la $a0, msg2
syscall #menampilkan message 2
move $a0,$t2
li $v0, 1
syscall #menampilkan hasil

mfhi $t0 #t0 = t0 mod t1
li $t1,20000

div $t0,$t1
mflo $t2 #t2 = t0 div t1
li $v0, 4
la $a0, msg3
syscall #menampilkan message 3
move $a0,$t2
li $v0, 1
syscall #menampilkan hasil

mfhi $t0 #t0 = t0 mod t1
li $t1,10000

div $t0,$t1
mflo $t2 #t2 = t0 div t1
li $v0, 4
la $a0, msg4
syscall #menampilkan message 4
move $a0,$t2
li $v0, 1
syscall #menampilkan hasil

mfhi $t0 #t0 = t0 mod t1
li $t1,5000
div $t0,$t1
mflo $t2 #t2 = t0 div t1
li $v0, 4
la $a0, msg5
syscall #menampilkan message 5
move $a0,$t2
li $v0, 1
syscall #menampilkan hasil

mfhi $t0 #t0 = t0 mod t1
li $t1,1000
div $t0,$t1
mflo $t2 #t2 = t0 div t1
li $v0, 4
la $a0, msg6
syscall #menampilkan message 6
move $a0,$t2
li $v0, 1
syscall #menampilkan hasil

message:

li $v0, 4
la $a0, msg7
syscall
li $v0,5
syscall
li $t1,1
beq $v0,$t1,main
bne $v0,$zero, message

exit:

li $v0, 4
la $a0, msg8
syscall
jr $ra

Print Screen hasilnya :

No comments: