150 likes | 267 Views
ECE 353 Introduction to Microprocessor Systems. Discussion 3. Topics. Subroutines/Stack Frames/Multi-Precision Arithmetic Q & A. Problem. Write a procedure that multiplies two 64-bit unsigned numbers and returns a 64-bit result in R1:R0. R1 – upper order bits R0 – lower order bits.
E N D
ECE 353Introduction to Microprocessor Systems Discussion 3
Topics • Subroutines/Stack Frames/Multi-Precision Arithmetic • Q & A
Problem • Write a procedure that multiplies two 64-bit unsigned numbers and returns a 64-bit result in R1:R0. • R1 – upper order bits • R0 – lower order bits
Problem • (0). Draw a diagram showing the complete execution of a 64-bit multiplication using 32-bit multiplies and generating a 128-bit result. (Note: We will only use 64-bits of the result.) A B X C D ------------------------------------------------------------------------------------------------------------------------ (B*D)H (B*D)L (A*D)H (A*D)L (B*C)H (B*C)L (A*C)H (A*C)L XX XX R1 R0
Problem • (1). Write a main code fragment that calls the subroutine (mul64). • Use a standard stack frame to pass two double-word parameters to the subroutine. Place them on the stack so that they are in little endian format. Put operand 1 on first. • Clean up the stack after the subroutine returns.
Problem (1). ; Filename: main5.s • ; Author: ECE 353 Staff • ; Description: main program file for discussion 6 • ARM ;use ARM instruction set • EXTERN mul64 • EXPORT __main • AREA FLASH, CODE, READONLY • __main • ;mul64 exercise • MOV R0, #0x00000055 ;operand1 MSW • PUSH {R0} • MOV R0, #0xC0000005 ;operand1 LSW • PUSH {R0} • MOV R0, #0x000000FF ;operand2 MSW • PUSH {R0} • MOV R0, #0x30000009 ;operand2 LSW • PUSH {R0} • BL mul64 • ADD SP, #16 ;cleanup stack • B __main • END
Problem • (2). Draw a diagram of the stack at the point when the call is made to mul64. (Note: the stack is drawn as 32-bits in width.)
Answer SP -> OP1 MSW OP1 LSW OP2 MSW OP2 LSW SP ->
Problem • (3). Write the complete subroutine that performs 64-bit unsigned multiplication, returning a 64-bit result. • Use a standard stack frame to retrieve the source operands. • Place the 64-bit result in R1:R0 • Allocate space for any temporary storage on the stack frame (do not use registers)(three words needed).
Problem (2). • ; Filename: mul64.s • ; Author: ECE 353 Staff • ; Description: Implementation of 64-bit multiply function • ARM ;use ARM instruction set • EXPORT mul64 • AREA FLASH, CODE, READONLY • ; Name: mul64 • ; Description: 64 x 64 unsigned multiply • ; Assumes: Arguments pushed on stack in little-endian form • ; first pushed - 64-bit operand1 • ; second pushed - 64-bit operand2 • ; Returns: R1:R0 - 64-bit product (truncated) • ; Modifies: None • mul64 • END
(2). mul64 PUSH {R11,LR} ;context save MOV R11, R13 ;get frame pointer SUB R13, R13, #12 ;allocate local variable space ;B*D LDR R0, [R11, #16] ;get operand1 LSW (B) LDR R1, [R11, #8] ;get operand2 LSW (D) UMULL R0, R1, R0, R1 ;BDH:BDL in R1:R0 STR R1, [R11, #-4] ;BDH to temp1 (local) STR R0, [R11, #-8] ;BDL to temp2 (local) ;A*D LDR R0, [R11, #8] ;get operand2 LSW (D) LDR R1, [R11, #20] ;get operand1 MSW (A) MUL R0, R0, R1 ;R0 = ADL STR R0, [R11, #-12] ;ADL to temp3 (local) ;B*C LDR R0, [R11, #16] ;get operand1 LSW (B) LDR R1, [R11, #12] ;get operand2 MSW (C) MUL R1, R0, R1 ;R1 = BCL ;add up MSW of result LDR R0, [R11, #-12] ;get temp3 (ADL) ADD R1, R1, R0 ;R1 = BCL + ADL LDR R0, [R11, #-4] ;get temp1 (BDH) ADD R1, R1, R0 ;R1 = BCL + ADL + BDH LDR R0, [R11, #-8] ;get temp2 (BDL) ;cleanup and exit ADD R13, R13, #12 ;deallocate local variable space mul64_exit POP {R11,PC} ;restore context and return END
Problem • (4). Update the drawing in part (2) to reflect the total stack frame used by the subroutine.
Answer SP -> OP1 MSW OP1 LSW OP2 MSW OP2 LSW [FP + 20] [FP + 16] [FP + 12] [FP + 8] SP -> R14 R11 temp1 temp2 temp3 FP -> [FP - 4] [FP - 8] [FP - 12] SP ->