Advertisement
Guest User

mirror.s

a guest
Aug 29th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.             .data 0x10000000
  2. num:        .word 11288511
  3. is_mirror:  .byte 0
  4. num_digits: .word 0
  5. mirror_val: .byte 0,1,5,'x','x',2,'x','x',8,'x'
  6.            
  7.             .globl __start
  8.             .text 0x00400000
  9.            
  10.  
  11. __start:    la   $t0,num
  12.             lw   $t0,0($t0) # N
  13.            
  14.             ## Extract digits in $t2 ##
  15.             li   $s0,0      # Counter
  16.             move $t3,$t0    # Take aux from num
  17.             li   $t2,10     # Save 10 constant
  18. $L1:        div  $t3,$t2    # Num / 10
  19.             mflo $t3        # Mov quocient to aux
  20.             addi $s0,$s0,1  # Increment digit counter
  21.             bnez $t3,$L1
  22.             la $t3,num_digits
  23.             sw $s0,0($t3)   # Save digit counter
  24.             ## Mirror process ##
  25.             move $t1,$s0     # $t1 = c  (length)
  26.             li   $t3,1       # $t3 = control signal (0->Not mirror,1->Mirror)
  27. $L2:        div  $t0,$t2     # num%10 ( While num > 9 -has more than one digit- )
  28.             mfhi $s0         # take remainder from hi
  29.             addiu $t1,$t1,-1         # to calculate this pow, it's necessary to rest 1 to $t1 (length)
  30.            jal $PowTen
  31.            addiu $t1,$t1,1         # restore length
  32.            move $s1,$v0     # s1 = (10**(c-1))
  33.            div  $t0,$s1     # n/(10**(c-1))
  34.            mflo $s1         # s1 = n/10**(c-1)
  35.            la $s2,mirror_val
  36.            add $s2,$s2,$s1  # mirror_val offset to read byte
  37.            lb $s1,0($s2)    # s1 = mirrored value
  38.            bne $s1,$s0,$EndFalse      # if n%10 != mirror[n/(10**(c-1))]
  39.            div $t0,$t2      # n /= 10
  40.            mflo $t0         # t0 = t0 / 10
  41.            addi $t1,$t1,-2  # $t1 -= 2 (c-2)
  42.            jal $PowTen      # (10**(c-2))
  43.            move $s1,$v0     # s1 = (10**(c-2))
  44.            li $s3,9         # 9 constant
  45.            bgt $t0,$s3,$L2 # if num < 10
  46.            ## if comes here, is equal in mirror ##
  47.            li $t0,1
  48.            la $t1,is_mirror
  49.            sb $t0,0($t1)
  50.            jal exit        
  51. $EndFalse:  li $t0,0
  52.             la $t1,is_mirror
  53.             sb $t0,0($t1)    # control signal = 0 -> X
  54.            jal exit
  55.            
  56.            ## Pow 10         ##
  57. $PowTen:    move $a0,$t1     # Save length for counter
  58.            li $s1,10        
  59. $LPT:       mult $s1,$t2     # $s1 = 10*10
  60.             mflo $s1         # $s1 = 10*10
  61.            addi $a0,$a0,-1  # Dec counter
  62.            bnez $a0,$LPT
  63.            move $v0,$s1
  64.            jr $ra
  65.            
  66.             ## Exit ##
  67. exit:       li $v0,10
  68.             syscall
  69. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement