Question: To practice writing assembly and passing parameters, you will implement the math functions of a calculator. > 6 / 3 2 > 7 / 3

To practice writing assembly and passing parameters, you will implement the math functions of a calculator.
>6/32>7/32>5/31>-5/3-1>-6/-32>3^29>2^416 i would like help with my divd.s function which takes postive numbers but i cant get negative numbers to work just get 0 and when both numbers are nagtive it freezes and i am not able to use neg in my language. Parameters are passed to functions in the registers r15, r14, r13 and r12 in that order (something special happens if there are more than 4 arguments to a function). The functions you are implementing are receiving the first number (left of operator) in r15 and the second number (right of operator) in r14. Remember, when the microcontroller executes the ret instruction at the end of a function, the return value should be in r15!mov src, dst ; move the contents of src to dst sub src, dst ; subtract the value in src from dst and store the result in dst add src, dst ; add the value in src to the value in dst and store the result in dst clr dst ; zero out destination (same as mov #0, dst) cmp a, b ; make the comparison: b [<,>=,==,!=] a jl label ; jump to label if b < a jge label ; jump to label if b >= a jeq label ; jump to label if b == a jne label ; jump to label if b != a jn label ; jump to label if N is set (result of previous instruction was negative) jmp label ; jump to label unconditionally (always).file "func/divd.S"
.text
.global divd
divd:
clr r13 ; Clear r13 to use it as a counter
clr r10 ; Clear r10 to use it as a sign flag
cmp #0, r14
jeq divd_zero ; If divisor is zero, jump to divd_zero
mov r15, r12 ; Copy dividend to r12
mov r14, r11 ; Copy divisor to r11
; Handle negative dividend
cmp #0, r15
jge check_divisor
mov r15, r12
sub r12, r12 ; Negate r12(r12=-r15)
mov #1, r10 ; Set sign flag for result
check_divisor:
; Handle negative divisor
cmp #0, r14
jge divd_loop
mov r14, r11
sub r11, r11 ; Negate r11(r11=-r14)
xor #1, r10 ; Toggle sign flag for result
divd_loop:
sub r11, r12 ; Subtract divisor from dividend
jn end_divd_loop ; If result is negative, jump to end_divd_loop
add #1, r13 ; Increment the counter
jmp divd_loop ; Repeat the loop
end_divd_loop:
cmp #0, r10
jeq positive_result
mov r13, r12
sub r12, r12 ; Negate r13(r13=-r13)
mov r12, r13
positive_result:
mov r13, r15 ; Move the counter to r15(quotient)
ret
divd_zero:
clr r15 ; If divisor is zero, set result to zero
ret
.global powr
powr:
clr r12 ; Clear r12 to store the result
mov #1, r12 ; Initialize result to 1(anything^0=1)
cmp #0, r14 ; Compare exponent (r14) to 0
jz _pow_end ; If exponent is 0, return 1
_pow_loop:
mov r15, r13 ; Move base (r15) into r13
call #mul ; Call multiplication subroutine (r12= r12* r15)
mov r12, r15 ; Move result of multiplication back to r15
sub #1, r14 ; Decrement exponent (r14)
jnz _pow_loop ; Continue if exponent is not zero
_pow_end:
mov r12, r15 ; Move the result to r15(result of a^b)
ret
.file "func/mul.S"
.text
.global mul
mul:
clr r12
mul_loop:
cmp #0, r14
jeq end_mul_loop
add r15, r12
dec r14
jmp mul_loop
end_mul_loop:
mov r12, r15 ;return result in r15
ret

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!