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.
i would like help with my divd.s function which takes postive numbers but i cant get negative numbers to work just get 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 r r r and r in that order something special happens if there are more than arguments to a function The functions you are implementing are receiving the first number left of operator in r and the second number right of operator in r Remember, when the microcontroller executes the ret instruction at the end of a function, the return value should be in rmov 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 # 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 alwaysfile "funcdivdS
text
global divd
divd:
clr r ; Clear r to use it as a counter
clr r ; Clear r to use it as a sign flag
cmp # r
jeq divdzero ; If divisor is zero, jump to divdzero
mov r r ; Copy dividend to r
mov r r ; Copy divisor to r
; Handle negative dividend
cmp # r
jge checkdivisor
mov r r
sub r r ; Negate rrr
mov # r ; Set sign flag for result
checkdivisor:
; Handle negative divisor
cmp # r
jge divdloop
mov r r
sub r r ; Negate rrr
xor # r ; Toggle sign flag for result
divdloop:
sub r r ; Subtract divisor from dividend
jn enddivdloop ; If result is negative, jump to enddivdloop
add # r ; Increment the counter
jmp divdloop ; Repeat the loop
enddivdloop:
cmp # r
jeq positiveresult
mov r r
sub r r ; Negate rrr
mov r r
positiveresult:
mov r r ; Move the counter to rquotient
ret
divdzero:
clr r ; If divisor is zero, set result to zero
ret
global powr
powr:
clr r ; Clear r to store the result
mov # r ; Initialize result to anything
cmp # r ; Compare exponent r to
jz powend ; If exponent is return
powloop:
mov r r ; Move base r into r
call #mul ; Call multiplication subroutine r r r
mov r r ; Move result of multiplication back to r
sub # r ; Decrement exponent r
jnz powloop ; Continue if exponent is not zero
powend:
mov r r ; Move the result to rresult of ab
ret
file "funcmulS
text
global mul
mul:
clr r
mulloop:
cmp # r
jeq endmulloop
add r r
dec r
jmp mulloop
endmulloop:
mov r r ;return result in r
ret
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
