CS代写 ; Your Name:

; Your Name:
; Partner’s Name (if any):
; (Both partner’s must also submit a partners.txt file.)

Copyright By PowCoder代写 加微信 powcoder

; TO RECEIVE CREDIT you must certify your work
; AND indicate which subroutines you’ve successfully completed.
; Mark the boxes below [ ] by replacing the space with an X as in [X].
; [ ] I/we certify that the code added herein is my/our own authorship –
; I/we have not used anyone else’s code (in whole or in part) for
; my/our modifications, and I/we have not shared my/our code in any
; form with anyone else.
; COMPLETE THE SUBROUTINES IN THIS ORDER:
; [ ] 1. subroutine MUL3ADD1 successfully completed
; [ ] 2. subroutine DIVIDEBY2 successfully completed
; [ ] 3. subroutine COLLATZ_STEPS successfully completed

; —————————————————–
; Copyright (c) 2022 – All Rights Reserved
; Posting this publicly is prohibited.
; —————————————————–

.ORIG x3000
BRnzp TEST_BEGIN

; STEP 1: MUL3ADD1
; ****************
; Multiplies an integer A by 3 and then adds 1.
; argument: in R0 is A
; return value: in R1 will be the result of (A * 3) + 1
; ************************* Start your code here *************************
; ************************** End your code here **************************

; STEP 2: DIVIDEBY2
; *****************
; Divides even positive integer N by 2.
; argument: in R0 is N
; return value: in R1 will be the result of N / 2
; ************************* Start your code here *************************
; ************************** End your code here **************************

; STEP 3: COLLATZ_STEPS
; *********************
; Calculates the number of steps to reach 1 from positive integer X.
; argument: in R0 is X
; return value: in R1 will be the number of steps to reach 1
COLLATZ_STEPS
; ************************* Start your code here *************************
; ************************** End your code here **************************

; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
; !!!!! DO NOT REMOVE OR MODIFY ANYTHING BELOW THIS LINE! !!!!!
; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

; MAIN SECTION
; ************
; Contains helper subroutines and testing code
; We’ve provided 3 subroutines to help check and debug your implementation:
; 1. PRINT_DIGIT
; 2. PRINT_INT
; 3. COLLATZ
; *************************************************************************
; Except for the labels to call the 3 helper subroutines listed above,
; DO NOT USE ANY OF THE OTHER LABELS in our code below.

; HELPER 1: PRINT_DIGIT
; *********************
; Prints a single-digit integer N, where N is between 0 and 9,
; arguments: in R0 is N
PRINT_DIGIT
; *************************************************************************
ST R0, PRINT_DIGIT_R0
ST R1, PRINT_DIGIT_R1
ST R7, PRINT_DIGIT_R7

LD R1, ASCII_ZERO
ADD R0, R0, R1

LD R0, PRINT_DIGIT_R0
LD R1, PRINT_DIGIT_R1
LD R7, PRINT_DIGIT_R7

PRINT_DIGIT_R0 .FILL x0000
PRINT_DIGIT_R1 .FILL x0000
PRINT_DIGIT_R7 .FILL x0000
ASCII_ZERO .FILL x0030
; *************************************************************************

; HELPER 2: PRINT_INT
; *******************
; Prints a two-digit integer N followed by a new line character (x000A).
; N is between 0 and 99. For N < 10, the leading 0 is printed. ; This subroutine must call PRINT_DIGIT to print a single digit. ; arguments: in R0 is N ; ************************************************************************* ST R0, PRINT_INT_R0 ST R1, PRINT_INT_R1 ST R2, PRINT_INT_R2 ST R7, PRINT_INT_R7 AND R1, R1, #0 SUBTRACT_TEN ADD R0, R0, #-10 BRn SUBTRACT_TEN_END ADD R1, R1, #1 BR SUBTRACT_TEN SUBTRACT_TEN_END ADD R2, R0, #10 AND R0, R1, R1 JSR PRINT_DIGIT AND R0, R2, R2 JSR PRINT_DIGIT LD R0, NEW_LINE LD R0, PRINT_INT_R0 LD R1, PRINT_INT_R1 LD R2, PRINT_INT_R2 LD R7, PRINT_INT_R7 PRINT_INT_R0 .FILL x0000 PRINT_INT_R1 .FILL x0000 PRINT_INT_R2 .FILL x0000 PRINT_INT_R7 .FILL x0000 NEW_LINE .FILL x000A ; ************************************************************************* ; HELPER 3: COLLATZ ; ***************** ; Subroutine that prints the Collatz steps taken for N to 1. ; Uses the COLLATZ_STEPS subroutine to calculate the number of steps. ; argument: in R0 is N, a positive integer ; ************************************************************************* ST R0, COLLATZ_R0 ST R2, COLLATZ_R2 ST R7, COLLATZ_R7 AND R0, R0, R0 ; Loop runs from N to 1 LOOP_CONDITION ; Run the loop while R0 >= 1
BRnz COLLATZ_LOOP_END

JSR COLLATZ_STEPS

ADD R2, R0, #0
ADD R0, R1, #0

JSR PRINT_INT

ADD R0, R2, #0

; Decrement number
ADD R0, R0, #-1

BRnzp LOOP_CONDITION
COLLATZ_LOOP_END

LD R0, COLLATZ_R0
LD R2, COLLATZ_R2
LD R7, COLLATZ_R7

COLLATZ_R0 .FILL x0000
COLLATZ_R2 .FILL x0000
COLLATZ_R7 .FILL x0000
; *************************************************************************

TEST_BEGIN

; ************************ TEST FOR MUL3ADD1 ******************************
; initialize the registers for checking callee saving
LD R2, INIT_REG_VAL1
LD R3, INIT_REG_VAL1
LD R4, INIT_REG_VAL1
LD R5, INIT_REG_VAL1
LD R6, INIT_REG_VAL1

; print the message
LEA R0, MUL3ADD1_MSG

; test the MUL3ADD1 subroutine to multiply a number with 3 and add 1
AND R0, R0, #0
LD R1, MUL3ADD1_N
ADD R0, R0, R1 ; initialize the operand to 32
JSR MUL3ADD1 ; calculate and store the result in R1
AND R0, R1, R1 ; move the result of MUL from R1 to R0
JSR PRINT_INT ; print the value in R0

; check if the original values in the registers are preserved
LD R0, INIT_REG_VAL_NEG1
ADD R3, R3, R0
BRnp MUL3ADD1_REG_SAVING_FAILED
ADD R4, R4, R0
BRnp MUL3ADD1_REG_SAVING_FAILED
ADD R5, R5, R0
BRnp MUL3ADD1_REG_SAVING_FAILED
ADD R6, R6, R0
BRnp MUL3ADD1_REG_SAVING_FAILED

BRnzp TEST_MUL3ADD1_END

MUL3ADD1_REG_SAVING_FAILED
LEA R0, MUL3ADD1_REG_SAVING_MSG
BRnzp TEST_MUL3ADD1_END

MUL3ADD1_MSG .STRINGZ “\n32 * 3 + 1 = ”
MUL3ADD1_REG_SAVING_MSG .STRINGZ “\nCheck for callee saving failed!\n”
INIT_REG_VAL1 .FILL x600D ; DO NOT USE THIS LABEL/VALUE
INIT_REG_VAL_NEG1 .FILL #-24589 ; DO NOT USE THIS LABEL/VALUE
MUL3ADD1_N .FILL #32 ; DO NOT USE THIS LABEL/VALUE

TEST_MUL3ADD1_END
; *************************************************************************

; ********************** TEST FOR DIVIDEBY2 *******************************
; initialize the registers for checking callee saving
LD R2, INIT_REG_VAL2
LD R3, INIT_REG_VAL2
LD R4, INIT_REG_VAL2
LD R5, INIT_REG_VAL2
LD R6, INIT_REG_VAL2

; print the message
LEA R0, DIVIDEBY2_MSG

; test the DIVIDEBY2 subroutine to Divide a number by 2
AND R0, R0, #0
LD R1, DIVIDEBY2_N ; load operand value from memory
ADD R0, R0, R1 ; initialize the operand
JSR DIVIDEBY2 ; divide and store the result in R1
AND R0, R1, R1 ; move the result of DIVIDEBY2 from R1 to R0
JSR PRINT_INT ; print the value in R0

; check if the original values in the registers are preserved
LD R0, INIT_REG_VAL_NEG2
ADD R2, R2, R0
BRnp DIVIDEBY2_REG_SAVING_FAILED
ADD R3, R3, R0
BRnp DIVIDEBY2_REG_SAVING_FAILED
ADD R4, R4, R0
BRnp DIVIDEBY2_REG_SAVING_FAILED
ADD R5, R5, R0
BRnp DIVIDEBY2_REG_SAVING_FAILED
ADD R6, R6, R0
BRnp DIVIDEBY2_REG_SAVING_FAILED

BRnzp TEST_DIVIDEBY2_END

DIVIDEBY2_REG_SAVING_FAILED
LEA R0, DIVIDEBY2_REG_SAVING_MSG
BRnzp TEST_DIVIDEBY2_END

DIVIDEBY2_MSG .STRINGZ “\n196 / 2 = ”
DIVIDEBY2_REG_SAVING_MSG .STRINGZ “\nCheck for callee saving failed!\n”
INIT_REG_VAL2 .FILL x600D ; DO NOT USE THIS LABEL/VALUE
INIT_REG_VAL_NEG2 .FILL #-24589 ; DO NOT USE THIS LABEL/VALUE
DIVIDEBY2_N .FILL #196 ; DO NOT USE THIS LABEL/VALUE

TEST_DIVIDEBY2_END
; *************************************************************************

; ************************** TEST FOR COLLATZ_STEPS ***********************
; initialize the registers for checking callee saving
LD R2, INIT_REG_VAL3
LD R3, INIT_REG_VAL3
LD R4, INIT_REG_VAL3
LD R5, INIT_REG_VAL3
LD R6, INIT_REG_VAL3

; print the message
LEA R0, COLLATZ_STEPS_MSG

AND R0, R0, #0
LD R1, COLLATZ_STEPS_N ; load operand from memory (= 1982)
ADD R0, R0, R1 ; initialize the operand
JSR COLLATZ_STEPS ; calculate & store the number of steps in R1
AND R0, R1, R1 ; move the result of COLLATZ_STEPS: R1 -> R0
JSR PRINT_INT ; print the value in R0

; check if the original values in the registers are preserved
LD R0, INIT_REG_VAL_NEG3
ADD R2, R2, R0
BRnp COLLATZ_STEPS_REG_SAVING_FAILED
ADD R3, R3, R0
BRnp COLLATZ_STEPS_REG_SAVING_FAILED
ADD R4, R4, R0
BRnp COLLATZ_STEPS_REG_SAVING_FAILED
ADD R5, R5, R0
BRnp COLLATZ_STEPS_REG_SAVING_FAILED
ADD R6, R6, R0
BRnp COLLATZ_STEPS_REG_SAVING_FAILED

BRnzp TEST_COLLATZ_STEPS_END

COLLATZ_STEPS_REG_SAVING_FAILED
LEA R0, COLLATZ_STEPS_REG_SAVING_MSG
BRnzp TEST_COLLATZ_STEPS_END

COLLATZ_STEPS_MSG .STRINGZ “\nSteps for reaching 1 from 1982 = ”
COLLATZ_STEPS_REG_SAVING_MSG .STRINGZ “\nCheck for callee saving failed!\n”
INIT_REG_VAL3 .FILL x600D ; DO NOT USE THIS LABEL/VALUE
INIT_REG_VAL_NEG3 .FILL #-24589 ; DO NOT USE THIS LABEL/VALUE
COLLATZ_STEPS_N .FILL #1982 ; DO NOT USE THIS LABEL/VALUE
TEST_COLLATZ_STEPS_END
; *************************************************************************

; ************************ USING COLLATZ **********************************
LEA R0, COLLATZ_MSG

LD R0, COLLATZ_N
JSR COLLATZ

BRnzp COLLATZ_EXEC_END

COLLATZ_N .FILL #15 ; DO NOT USE THIS LABEL/VALUE
COLLATZ_MSG .STRINGZ “\nNumber of Collatz Steps for 15, 14, 13, …, 1\n”
COLLATZ_EXEC_END
; *************************************************************************

HALT ; stop the program execution

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com