-2

I need an assembly mips code that asks the user for 2 ints and calculate the exponential, for example: they put 2 and 8 and the result is 256, because 2^8 = 256.

I know how to ask for the numbers:

.data
    prompt1: .asciiz "X: "
    prompt2: .asciiz "Y: "
    result: .ascii "Result: "

but I don't know how to use them or how to make x^y

Kiiush
  • 1
  • Requests to do a complete homework assignment are not appropriate. You can ask a question in the context of homework, if you show your work and describe where you are stuck. What you're showing doesn't meet this criteria, in particular since there is no code attempt. If you're stuck at the beginning, seek out instructors as a question post here won't be able to teach assembly language programming. – Erik Eidt Nov 18 '22 at 14:59
  • I don't think there are any computers that have a dedicated exponent operator, they have to actually multiply the numbers out. I'd imagine it would be a simple loop where you multiply the first number by itself. As for numbers raised to the zero power, you'll have to make a special case to return 1. – puppydrum64 Nov 23 '22 at 18:40

1 Answers1

0

If both of the integers are unsigned or positive at all times what you can do is (Inputs are not from user in this case you should add syscalls for the first 2 li instructions in order to do that):

$a0: x variable $a1: y variable

    .text
li  $a0, 5 #x variable
li  $a1, 3 #y variable
jal exponent
li  $v0, 10
syscall

    exponent:
addi    $sp, $sp, -12   #decrement the stack pointer
sw  $s0, 0($sp) #store values for not muting
sw  $s1, 4($sp)
sw  $s2, 8($sp)

move    $s0, $a0
move    $s1, $a1
li  $s2, 1
    ###PRINT###
la  $a0, prompt1
li  $v0, 4
syscall
move    $a0, $s0
li  $v0, 1
syscall
la  $a0, prompt2
li  $v0, 4
syscall
move    $a0, $s1
li  $v0, 1
syscall
    ###END PRINT###
    loop:           
mul $s2, $s2, $s0   #in each iteration 1 multiplication done
addi    $s1, $s1, -1
bne $s1, 0, loop    #when $a1 equals 0 it will go on 

    ###PRINT###
la  $a0, result
li  $v0, 4
syscall
move    $a0, $s2
li  $v0, 1
syscall
    ###END PRINT###
move    $v0, $s2    
lw  $s0, 0($sp) #load values for not muting
lw  $s1, 4($sp)
lw  $s2, 8($sp)
addi    $sp, $sp, 12
jr  $ra

    .data
    .align 2
prompt1:    .asciiz     "X: "
    .align 2
prompt2:    .asciiz     "Y: "
    .align 2
result:     .asciiz     "Result: "

If you are aiming to cover negative numbers too you should add another loop for that case and inside it rather than mul instruction you should use div.

Keep in mind that in Stackoverflow you should not ask for whole programs :)

PoePew
  • 37
  • 7