Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- buffer: .space 32
- start_msg: .asciiz "Choisir le mode d'emploi\n1:\tValidation d'un numero CB.\n2:\tGenerer un numéro CB.\n3:\tSortir.\n"
- exit_msg: .asciiz "Programme terminé avec success\n"
- luhn: .asciiz "\n\nLuhn algorithm:"
- luhn_success: "success\n"
- luhn_failure: "echec\n"
- nl: .asciiz "\n"
- insert_cb_nb_msg: .asciiz "inserer un numéro CB: max 32 caracteres\n"
- .text
- .globl __main
- j __main # jump to __main
- __validate:
- li $v0, 9 # $v0 = 9
- la $a0, 32
- syscall # alocate 32 bytes
- move $s0, $v0 # $s0 = $v0 | cb_d
- # ask for cb
- li $v0, 4 # $v0 = 4
- la $a0, insert_cb_nb_msg # $a0 = insert_cb_nb_msg
- syscall
- #read cb
- li $v0, 8 # $v0 = 8
- la $a0, buffer # $a0 = buffer
- li $a1, 32 # $a1 = 32
- syscall
- move $s1, $a0 # $s1 = $v0 | cb
- # parse string to int (array)
- # $t1 -> size
- # $s1 -> address of cb ($s1)
- # $t2 -> address of cb_d ($s0)
- li $t1, 0 # $t1 = 0 int array size | i = 0
- move $t2, $s0
- move $t3, $s1 # $t2 = $s0 | $t2 = cb_d
- __validate_parse_loop:
- add $t3, $s1, $t1 # $s1 = $s1 + 1 | cb + 1
- lb $t6, 0($t3) # $t6 = itt[i]
- beq $t6, '\n', __validate_parse_loop_exit # if $t6 == 0 then __validate_parse_loop_exit
- subi $t6, $t6, 48 # $t6 = $t6 - 0x30
- sb $t6, 0($t2) #
- add $t2, $t2, 1 # $s0 = $s0 + 1 | cb_d + 1
- addi $t1, $t1, 1 # $t1 = $t1 + 1 | size++
- b __validate_parse_loop # branch to __validate_parse_loop
- __validate_parse_loop_exit:
- # subi $t1, $t1, 1 # $t1 = $t1 - 1
- ##############
- # set key to s3
- # set size to s2
- ##############
- move $s2 $t1 # $s2 = size
- add $t1 $s0 $s2
- sub $t1 $t1 1
- lb $s3, 0($t1) # s3 = key
- ################
- #### reverse array
- subi $s2, $s2, 2 # size -2 | escape key
- add $t1 $s0 $s2
- move $t0 $s0
- __validate_reverse_arr:
- lb $t2 0($t0)
- lb $t3 0($t1)
- sb $t2 0($t1)
- sb $t3 0($t0)
- addi $t0 $t0 1
- addi $t1 $t1 -1
- blt $t0 $t1 __validate_reverse_arr
- __validate_reverse_arr_exit:
- ######## test #####
- li $v0, 11 # system call #4 - print string
- la $a0, '\n'
- syscall # execute
- syscall # execute
- move $t2 $s0
- li $t3 0
- __print:
- bgt $t3 $s2 __print_exit
- add $t2 $s0 $t3
- lb $t4, 0($t2) #
- add $t4 $t4 48
- addi $t3, $t3, 1 # $t3 = $t3 + 1
- b __print
- __print_exit:
- ######### \test #####
- ################
- #todo: div modulo
- # etapes 3-5 Luhn alg
- li $t7 0 # somme = 0
- li $t6 1 # mod 2
- add $t1 $s0 $s2 # end
- move $t0 $s0 #start | t0 = cb_d
- __validate_mult_sub_loop:
- bgt $t0 $t1 __validate_mult_sub_loop_exit
- andi $t6 $t6 0x01 # mod 2
- lbu $t2 0($t0)
- beq $t6 0x0 __validate_mult_sub_loop_branch_even
- add $t2 $t2 $t2 # *cb_d *= 2
- bleu $t2, 0x09, __validate_mult_sub_loop_branch_even # if chifre *2 <= 9 then branch
- subu $t2 $t2 0x09
- __validate_mult_sub_loop_branch_even:
- addu $t7 $t7 $t2 # somme des chifres
- addi $t6 $t6 1
- addi $t0 $t0 1
- b __validate_mult_sub_loop
- __validate_mult_sub_loop_exit:
- li $t0 10
- div $t7 $t0
- mfhi $t1
- bnez $t1 __validate_mod_zero
- subu $t1 $t2 $t1 # t1 != 0
- __validate_mod_zero: # t1 = 0
- li $v0 4
- la $a0 luhn
- syscall
- beq $t1 $s3 __validate_l_succes
- la $a0 luhn_failure
- syscall
- b __validate_l_exit
- __validate_l_succes:
- la $a0 luhn_success
- syscall
- b __validate_l_exit
- __validate_l_exit:
- ######### test #####
- # li $v0, 11 # system call #4 - print string
- # la $a0, '\n'
- # syscall # execute
- # syscall # execute
- # move $t2 $s0
- # li $t3 0
- # __print:
- # bgt $t3 $s2 __print_exit
- # add $t2 $s0 $t3
- # lb $t4, 0($t2) #
- # add $t4 $t4 48
- # li $v0, 11 # system call #1 - print int
- # move $a0, $t4
- # syscall # execute
- # addi $t3, $t3, 1 # $t3 = $t3 + 1
- # b __print
- # __print_exit:
- # ######### \test #####
- #todo: free heap
- j _ask_loop
- ##### \validate
- __generate:
- li $v0 1
- li $a0 6
- syscall
- j _ask_loop
- __main:
- #ask for mode
- _ask_loop:
- li $v0 4
- la $a0 start_msg
- syscall
- #read mode (int)
- li $v0 5
- syscall
- beq $v0, 3, exit # if $v0 == 3 then exit
- beq $v0, 1, __validate # if $v0 == 1 then __validate
- beq $v0, 2, __generate # if $v0 == 2 then __generge
- b _ask_loop # branch to _ask_loop
- exit:
- li $v0, 4 # $v0 = 4
- la $a0, exit_msg #
- syscall
- li $v0, 10 # $v0 = 10
- syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement