2014年4月6日 星期日

MIPS 輸入10組密碼 如果是這十組密碼內 則成功

.data
value: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
msg1: .asciiz "correct\n“
msg2: .asciiz "incorrect please try again\n“
msg3: .asciiz "===========================\n“
.text
main:
 la $t0, value         #  array
 li $s1, 0        #  i
 li $t1, 0        #  index
 li $t2, 0        #  arr[index]
read:
 li $v0, 5
 syscall
 add $t2,$t1,$t0        #t2=arr[t1]
 sw $v0, 0($t2)         #store to array
 add $t1,$t1,4
 add $s1, $s1, 1        #  i++
 beq $s1, 10 , line  #  i<10?
 j read
line:
 li $v0, 4        # draw a line
 la $a0, msg3
 syscall
guess:
 li $s3, 0        #  k
 li $t3, 0        #  index2
 li $t4, 0        #  arr[index2]
 li $v0, 5
 syscall
inner_for:
 add $t4,$t3,$t0
 lw  $t5,0($t4)        #arr[index2] load to $t5
 bne $v0,$t5,ER
 li $v0, 4
 la $a0, msg1  #print correct
 syscall
 j guess   #restart guess
ER:
 add $s3, $s3, 1        #  k++
 add $t3,$t3,4
 beq $s3, 10 , while  #  k<10?
 j inner_for
while:
 li $v0, 4
 la $a0, msg2  #print incorrect
 syscall
 j guess

2014年4月2日 星期三

LISP

 (fast 2 * n 1)  ==> 2^n
 (fast 2 + n 0)  ==> 2*n

(define (fast a op n unit)
 (define (square x) (op x x))
 (define (iter a n result)
         (cond ((= n 0) result)
               ((even? n) (iter (square a) (/ n 2) result))
               (else (iter a (- n 1) (op a result)))))
(iter a n unit))