מחשבון באסבמלי

bennyassembly, mips assemblyLeave a Comment

מחשבון באסבמלי הכולל: פונקציית כפל, חיבור, וחזקה, רק על ידי פונקציית החיבור בלבד , תרגול שימוש בפונקציות על ידי פקודת jal ושמירת הערכים במחסנית לפני הכניסה לפונקציה ובתוכה כמקובל




.data 
 str0:.asciiz " enter tow numbers and operator:\n "

 
.text 
                                 # print str0
li $v0 4
la $a0 str0
syscall 
                                    # input the firse number
li $v0 5
syscall 
add $a0 $v0 $zero
                                   # input the cecond number
move $t7 $a0                       # keep the $a0 in tenporary register                              
li $v0 5
syscall 
add $a1 $v0 $zero                                  
li $v0 12                           # input the operator
syscall 
                                    #save the operators
li $t0 '+'
li $t1 '*'
li $t2 '^'                                    
beq $t0 $v0 go_add           #if the operator is + go to go_add
beq $t1 $v0 go_mult          #if the operator is * go to go_mult
beq $t2 $v0 go_pow           #if the operator is ^ go to go_pow

go_add:
jal fadd
j print

go_mult:
li $t4 10                   # t4 = 10       to know if we are in multi status ...
addi $t8 $t4 0              # t8 = t4       .. if(t8 = t4 ) {we are in the multi situation }

#----------push-to-stack------------#
addi $sp $sp -4
sw $t1 ($sp)
addi $sp $sp -4
sw $a1 ($sp)
jal fmult
#---------pop-from-stack----------#
lw $a1 ($sp)
addi $sp $sp 4
lw $t1 ($sp)
addi $sp $sp 4

j print

go_pow:                             #------------------------------------------------
beq $a1 $zero pow_0                 #       if the exponent is 0 put 1 in $vo and   #                                
j skip_pow_0                        #       jump to print the resalt = 1            #
pow_0:                              #       else                                    #
li $v0 1                            #       skip and continue                       #
j print                             #                                               #
skip_pow_0:                         #------------------------------------------------

li $t3 1                            #-----------------------------------------------#    
beq $a1 $t3 pow_1                   #      if the exponent is 1 put $a0             #
j skip_pow_1                        #      in $vo and jump to print the resalt = $a0#
pow_1:                              #     (first num = resalt)                      #
move $v0 $a0                        #      else                                     #
j print                             #      skip and continue                        #
skip_pow_1:                         #------------------------------------------------

li $t4 10              # t4 = 10       # t8 != t4               to know if we are in pow status


#----------push-to-stack---------------bifore function  (keep value of register a & t)
addi $sp $sp -4
sw $t6 ($sp)
addi $sp $sp -4
sw $a0 ($sp)
jal fpow
#---------pop-from-stack----------#
lw $a0 ($sp)
addi $sp $sp 4
lw $t6 ($sp)
addi $sp $sp 4


j print

fadd:                                     # additon function
add $v0  $a0 $a1
jr $ra 
 
fmult:
addi $sp $sp -4
sw $ra ($sp)

beq $t4 $t8 change_counter                # if(t8 = t4 ) {we are in the multi situation  (line  36)
addi $t1 $t7 0                            # inittalize the counter ($t1=$a0=first number)
j skip
change_counter:                      
addi $t1 $a1 0
skip:
li $a1 0                                  # to add the first number over and over agin
loop_add:
beq $t1 $zero end_loop_multi              # to put the resolt in vo for printing
jal fadd
addi $t1 $t1 -1                           # counter --
add $v1 $v0 $v1                           #  save the resolt in $v1
j loop_add
end_loop_multi:
addi $v0 $v1 0                           #put the resolt back in $v0
li $v1 0                                 # for the next loop
lw $ra 0($sp)
addi $sp $sp 4
jr $ra
fpow:
#----------push-to-stack-------------in-the-function (keep value of register s & ra)
addi $sp $sp -4                           
sw $s2 ($sp)                       
addi $sp $sp -4     
sw $ra ($sp)                          # push-to-stack for save  adress to return when func is finish
addi $t6 $a1 -1
loop_2:
beq $t6 $zero end_loop_pow

#----------push-to-stack-------------bifore-function  (keep value of register a & t)
addi $sp $sp -4
sw $t1 ($sp)
addi $sp $sp -4
sw $a1 ($sp)
jal fmult
#---------pop-from-stack----------#
lw $a1 ($sp)
addi $sp $sp 4
lw $t1 ($sp)
addi $sp $sp 4

addi $t6 $t6 -1
move $s2 $v0         # save the resalt (from the multi func)   {3^3 --> $s2 = 3*3 =3+3+3= 9   }
move $a0 $s2          # put the resalt in $a0 for the next mult .. ($a0 = 9 --> (9)*3 ...)
j loop_2

end_loop_pow:

move $v0 $s2
#----------pop-from-stack-------------in-the-function (restore value of register s & ra)
lw $ra ($sp)
addi $sp $sp 4
lw $s2 ($sp)
addi $sp $sp 4
jr $ra
print:
add $a0 $v0 $zero
li $v0 1
syscall 



כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *