Implement ADD16, SUB16, MUL24, and COMPOUND. Skeleton code provided. Please resp
ID: 3601961 • Letter: I
Question
Implement ADD16, SUB16, MUL24, and COMPOUND. Skeleton code provided. Please response with complete code
.include "m128def.inc" ; Include definition file ;*********************************************************** ;* Internal Register Definitions and Constants ;*********************************************************** .def mpr = r16 ; Multipurpose register .def rlo = r0 ; Low byte of MUL result .def rhi = r1 ; High byte of MUL result .def zero = r2 ; Zero register, set to zero in INIT, useful for calculations .def A = r3 ; A variable .def B = r4 ; Another variable .def oloop = r17 ; Outer Loop Counter .def iloop = r18 ; Inner Loop Counter ;*********************************************************** ;* Start of Code Segment ;*********************************************************** .cseg ; Beginning of code segment ;----------------------------------------------------------- ; Interrupt Vectors ;----------------------------------------------------------- .org $0000 ; Beginning of IVs rjmp INIT ; Reset interrupt .org $0046 ; End of Interrupt Vectors ;----------------------------------------------------------- ; Program Initialization ;----------------------------------------------------------- INIT: ; The initialization routine ; Initialize Stack Pointer ; TODO ; Init the 2 stack pointer registers clr zero ; Set the zero register to zero, maintain ; these semantics, meaning, don't ; load anything else into it. ;----------------------------------------------------------- ; Main Program ;----------------------------------------------------------- MAIN: ; The Main program ; Setup the ADD16 function direct test ; (IN SIMULATOR) Enter values 0xA2FF and 0xF477 into data ; memory locations where ADD16 will get its inputs from ; (see "Data Memory Allocation" section below) ; Call ADD16 function to test its correctness ; (calculate A2FF + F477) ; Observe result in Memory window ; Setup the SUB16 function direct test ; (IN SIMULATOR) Enter values 0xF08A and 0x4BCD into data ; memory locations where SUB16 will get its inputs from ; Call SUB16 function to test its correctness ; (calculate F08A - 4BCD) ; Observe result in Memory window ; Setup the MUL24 function direct test ; (IN SIMULATOR) Enter values 0xFFFFFF and 0xFFFFFF into data ; memory locations where MUL24 will get its inputs from ; Call MUL24 function to test its correctness ; (calculate FFFFFF * FFFFFF) ; Observe result in Memory window ; Call the COMPOUND function ; Observe final result in Memory window DONE: rjmp DONE ; Create an infinite while loop to signify the ; end of the program. ;*********************************************************** ;* Functions and Subroutines ;*********************************************************** ;----------------------------------------------------------- ; Func: ADD16 ; Desc: Adds two 16-bit numbers and generates a 24-bit number ; where the high byte of the result contains the carry ; out bit. ;----------------------------------------------------------- ADD16: ; Load beginning address of first operand into X ldi XL, low(ADD16_OP1) ; Load low byte of address ldi XH, high(ADD16_OP1) ; Load high byte of address ; Load beginning address of second operand into Y ; Load beginning address of result into Z ; Execute the function ret ; End a function with RET ;----------------------------------------------------------- ; Func: SUB16 ; Desc: Subtracts two 16-bit numbers and generates a 16-bit ; result. ;----------------------------------------------------------- SUB16: ; Execute the function here ret ; End a function with RET ;----------------------------------------------------------- ; Func: MUL24 ; Desc: Multiplies two 24-bit numbers and generates a 48-bit ; result. ;----------------------------------------------------------- MUL24: ; Execute the function here ret ; End a function with RET ;----------------------------------------------------------- ; Func: COMPOUND ; Desc: Computes the compound expression ((D - E) + F)^2 ; by making use of SUB16, ADD16, and MUL24. ; ; D, E, and F are declared in program memory, and must ; be moved into data memory for use as input operands. ; ; All result bytes should be cleared before beginning. ;----------------------------------------------------------- COMPOUND: ; Setup SUB16 with operands D and E ; Perform subtraction to calculate D - E ; Setup the ADD16 function with SUB16 result and operand F ; Perform addition next to calculate (D - E) + F ; Setup the MUL24 function with ADD16 result as both operands ; Perform multiplication to calculate ((D - E) + F)^2 ret ; End a function with RET ;----------------------------------------------------------- ; Func: MUL16 ; Desc: An example function that multiplies two 16-bit numbers ; A - Operand A is gathered from address $0101:$0100 ; B - Operand B is gathered from address $0103:$0102 ; Res - Result is stored in address ; $0107:$0106:$0105:$0104 ; You will need to make sure that Res is cleared before ; calling this function. ;----------------------------------------------------------- MUL16: push A ; Save A register push B ; Save B register push rhi ; Save rhi register push rlo ; Save rlo register push zero ; Save zero register push XH ; Save X-ptr push XL push YH ; Save Y-ptr push YL push ZH ; Save Z-ptr push ZL push oloop ; Save counters push iloop clr zero ; Maintain zero semantics ; Set Y to beginning address of B ldi YL, low(addrB) ; Load low byte ldi YH, high(addrB) ; Load high byte ; Set Z to begginning address of resulting Product ldi ZL, low(LAddrP) ; Load low byte ldi ZH, high(LAddrP); Load high byte ; Begin outer for loop ldi oloop, 2 ; Load counter MUL16_OLOOP: ; Set X to beginning address of A ldi XL, low(addrA) ; Load low byte ldi XH, high(addrA) ; Load high byte ; Begin inner for loop ldi iloop, 2 ; Load counter MUL16_ILOOP: ld A, X+ ; Get byte of A operand ld B, Y ; Get byte of B operand mul A,B ; Multiply A and B ld A, Z+ ; Get a result byte from memory ld B, Z+ ; Get the next result byte from memory add rlo, A ; rlo <= rlo + A adc rhi, B ; rhi <= rhi + B + carry ld A, Z ; Get a third byte from the result adc A, zero ; Add carry to A st Z, A ; Store third byte to memory st -Z, rhi ; Store second byte to memory st -Z, rlo ; Store third byte to memory adiw ZH:ZL, 1 ; Z <= Z + 1 dec iloop ; Decrement counter brne MUL16_ILOOP ; Loop if iLoop != 0 ; End inner for loop sbiw ZH:ZL, 1 ; Z <= Z - 1 adiw YH:YL, 1 ; Y <= Y + 1 dec oloop ; Decrement counter brne MUL16_OLOOP ; Loop if oLoop != 0 ; End outer for loop pop iloop ; Restore all registers in reverves order pop oloop pop ZL pop ZH pop YL pop YH pop XL pop XH pop zero pop rlo pop rhi pop B pop A ret ; End a function with RET ;----------------------------------------------------------- ; Func: Template function header ; Desc: Cut and paste this and fill in the info at the ; beginning of your functions ;----------------------------------------------------------- FUNC: ; Begin a function with a label ; Save variable by pushing them to the stack ; Execute the function here ; Restore variable by popping them from the stack in reverse order ret ; End a function with RET ;*********************************************************** ;* Stored Program Data ;*********************************************************** ; Enter any stored data you might need here OperandD: .DW 0xFD51 ; test value for operand D OperandE: .DW 0x1EFF ; test value for operand E OperandF: .DW 0xFFFF ; test value for operand F ;*********************************************************** ;* Data Memory Allocation ;*********************************************************** .dseg .org $0100 ; data memory allocation for MUL16 example addrA: .byte 2 addrB: .byte 2 LAddrP: .byte 4 ; Below is an example of data memory allocation for ADD16. ; Consider using something similar for SUB16 and MUL24. .org $0110 ; data memory allocation for operands ADD16_OP1: .byte 2 ; allocate two bytes for first operand of ADD16 ADD16_OP2: .byte 2 ; allocate two bytes for second operand of ADD16 .org $0120 ; data memory allocation for results ADD16_Result: .byte 3 ; allocate three bytes for ADD16 result ;*********************************************************** ;* Additional Program Includes ;*********************************************************** ; There are no additional file includes for this program
Explanation / Answer
/** 16-bit Adder **/ //Adds two 16-bit values. //The most-significant carry bit is ignored. CHIP Add16 { IN a[16], b[16]; OUT out[16]; PARTS: //First, we add a and b together and output the sum. HalfAdder(a=a[0], b=b[0], sum=out[0], carry=carry0); //Next we carry over the carry from the previous operation, //and add it to the sum of the next operation in the sequence. //Each time, we output the sum and add the carry to the next //addition in the sequence. FullAdder(a=a[1], b=b[1], c=carry0, sum=out[1], carry=carry1); FullAdder(a=a[2], b=b[2], c=carry1, sum=out[2], carry=carry2); FullAdder(a=a[3], b=b[3], c=carry2, sum=out[3], carry=carry3); FullAdder(a=a[4], b=b[4], c=carry3, sum=out[4], carry=carry4); FullAdder(a=a[5], b=b[5], c=carry4, sum=out[5], carry=carry5); FullAdder(a=a[6], b=b[6], c=carry5, sum=out[6], carry=carry6); FullAdder(a=a[7], b=b[7], c=carry6, sum=out[7], carry=carry7); FullAdder(a=a[8], b=b[8], c=carry7, sum=out[8], carry=carry8); FullAdder(a=a[9], b=b[9], c=carry8, sum=out[9], carry=carry9); FullAdder(a=a[10], b=b[10], c=carry9, sum=out[10], carry=carry10); FullAdder(a=a[11], b=b[11], c=carry10, sum=out[11], carry=carry11); FullAdder(a=a[12], b=b[12], c=carry11, sum=out[12], carry=carry12); FullAdder(a=a[13], b=b[13], c=carry12, sum=out[13], carry=carry13); FullAdder(a=a[14], b=b[14], c=carry13, sum=out[14], carry=carry14); FullAdder(a=a[15], b=b[15], c=carry14, sum=out[15], carry=carry15); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.