a4q1.s

来自「ARM下 Implement matrix multiplication of 」· S 代码 · 共 456 行 · 第 1/2 页

S
456
字号
	swi	SWI_Close			; close file
	swi	SWI_Exit			; exit Program
	
	
;******** ReadMat *************************
; ReadMat (FileHandle,Size,MatrixAddress) passed by stack fp+8 fp+12 fp+16
; read matrix from input file and store to matrixaddress passed by parameters
ReadMat:
	stmfd	SP!, {r0-r10,FP,LR}		; store registers
	add	fp, sp, #44			; set frame point
	ldr	r1, [fp, #8]			; load input file handle
	ldr	r2, [fp, #12]			; load size
	ldr 	r3, [fp, #16]			; load pointer of matrix address to store
	
	mul	r4, r2, r2			; number of intergers to store 	
	mov	r5, #0				; Counter r5
;======================================================
;Each Read loop will read one integer from input file
;======================================================
Read:	
	mov	r0, r1
	swi	SWI_RdInt 			; read the integer
						; the integer is now in register R0
	bcs	Exit				; reach the end of data
	str	r0, [r3], #4			; store maxtrix to memory Address
	add	r5, r5, #1			; update counter
	cmp	r4, r5				; check if whole matrix is read
	bgt	Read				; if not read next integer

	ldmfd	SP!, {r0-r10,fp,pc}		; go back to main execute loop
	
;********* MatrixMultiply *************************
; ReadMat (FileHandle,Size,MatrixAddress) passed by stack fp+8 fp+12 fp+16
; read matrix from input file and store to matrixaddress passed by parameters
; r0= sum of each multiplication
; r1= max1address 
; r2= max2address 
; r3= max3address 
; r4= size 
; r5= row counter
; r6= column counter
; r7= counter for sum
; r8= row update factor = size*4
; r9= row offset
; r10=column offset
; r11=factor from matrix 1
; r12=factor from matrix 2

MatrixMultiply:
	stmfd	SP!, {r0-r10,fp,LR}		; store registers
	add	fp, sp, #44			; set frame point
	ldr	r1, [fp, #8]			; load matrix 1 address
	ldr	r2, [fp, #12]			; load matrix 2 address
	ldr 	r3, [fp, #16]			; load matrix 3 address
	ldr	r4, [fp, #20]			; load matrix size

	mov	r5, #0				; row counter
	mov 	r6, #0				; column counter
;====================================================
;each multLoop calculate one cell for result matrix
;====================================================
MultLoop:	
	mov	r0, #0				; initialize sum
	mov	r7, #0				; counter for sum
	mov	r8, r4, lsl #2			; row update factor
	mul	r9, r8,	r5			; row offset
	mov 	r10, r6, lsl #2			; column offset

;====================================================
;each multCell loop calculate the product of factor 
;from matrix 1 and factor from matrix 2 and add result
;to r0
;====================================================
	
MultCell:
	ldr	r11, [r1, r9]			; row factor
	ldr 	r12, [r2, r10]			; column factor
	mul	r14, r11, r12			; perform multiplication
	add	r0, r0, r14			; add to sum
	add	r7, r7, #1			; update sum counter
	add	r9, r9, #4			; update row offset
	add	r10, r10, r8			; update column offset
	cmp	r4, r7				; check if end of MultCell
	bgt	MultCell

;=================================
;update to next Cell calculation
;=================================
NextCell:
	str	r0, [r3], #4			; store current matrix cell and update
	add	r6, r6, #1			; update column counter
	cmp 	r4, r6				; check if end of row, (row counter update needed)
	bgt	MultLoop			; if not, calculate next matrix cell

;================================
;update to next Row
;================================	
NextRow:
	add	r5, r5, #1			; update row counter
	mov 	r6, #0				; reset column counter
	cmp	r4, r5				; check if end of matrix calculation
	bgt	MultLoop			; if not, calculate next matrix cell
;================================
;End of MatrixMat, return to main loop
;================================
EndOfMatrixMat:	
	ldmfd	SP!, {r0-r10,fp,pc}		; return to main loop
	


;******** PrintMat *************************
; outputs a Matrix on both console and output file
; PrintMat (FileHandle,MatrixAddress,Size) passed by stack fp+8 fp+12 fp+16
; r0= output file handle
; r3= pointer to Matrix Address
; r4= size of matrix
; r5= row counter
; r6= column counter

PrintMat:					; print matrix
	stmfd	SP!, {r0-r10,FP,LR}		; save registers
	add	fp, sp, #44			; set frame point
	ldr	r0, [fp, #8]			; load output file handle
	ldr	r3, [fp, #12]			; load pointer to Matrix Address
	ldr	r4, [fp, #16]			; load size of matrix

	mov	r5, #0				; initialize r5 row counter
	mov	r6, #0				; initialize r6 column counter

;===========================================
;Each PrintLoop print a cell of matrix
;===========================================
PrintLoop:					
	
	ldr	r1, [r3], #4			; integer on each matrix cell, update to next integer address
	stmfd	sp!,{r0,r1}			; pass parameter to stack
	bl	PrintInteger			; PrintInter (FileHandle, Interger) passed by stack
	add	sp, sp, #8			; clear stack
	ldr	r1, =Tab			; Tab String
	stmfd	sp!,{r0,r1}			; PrintString (FileHandle, StringAddress) passed by stack
	bl	PrintString			; PrintString(FileHandle,StringAddress),passed by stack
	add	sp, sp, #8			; clear stack
	
	add	r6, r6, #1			; update column counter
	cmp 	r4, r6				; compare to size
	bgt	PrintLoop			; if end of current row, go to next row
						; otherwise go to next PrintLoop
;===========================================
;update to next row of matrix 
;===========================================	
NextPrintRow:					
	ldr	r1, =Return			; Return String
	stmfd	sp!,{r0,r1}			; pass parameter to stack
	bl	PrintString			; PrintString(FileHandle,StringAddress),passed by stack
	add	sp, sp, #8			; clear stack
	
	add	r5, r5, #1			; update row counter
	mov 	r6, #0				; reset column counter
	cmp	r4, r5				; check end of matrix
	bgt	PrintLoop			; if not, go to print next integer
	ldmfd 	sp!, {r0-r10,fp,pc}		; if end, back to main loop
	
;******** PrintInteger *************************
; outputs a integer to both the console and to an output file
; PrintInteger (FileHandle,Integer) in stack fp+8 fp+12
; r2= output file handle
; r3= integer to print 

PrintInteger:
	stmfd	SP!, {r0-r10,FP,LR}		; save registers
	add	fp, sp, #44			; set frame point
	ldr	r2, [fp, #8]			; load output file handle
	ldr	r3, [fp, #12]			; load Integer to print
	
	mov 	r0, #Console			; print on screen
	mov	r1, r3
	swi	SWI_PrInt			; print integer
	
	mov	r0, r2				; print to output file
	swi	SWI_PrInt			; print integer
	ldmfd	SP!, {r0-r10,FP,PC}		; return to mainloop
	
	
;******** PrintString *************************
; outputs a string to both the console and to an output file
; PrintString (FileHandle,StringAddress) in stack fp+8 fp+12
; r2= output file handle
; r3= pointer to stringAddress

PrintString:
	stmfd	SP!, {r0-r10,FP,LR}		; save registers
	add	fp, sp, #44			; get frame point
	ldr	r2, [fp, #8]			; load output file handle
	ldr	r3, [fp, #12]			; load pointer to string to print
	
	mov 	r0, #Console			; print on screen
	mov	r1, r3
	swi	SWI_PrStr			; print string
	
	mov	r0, r2				; print to output file
	swi	SWI_PrStr			; print string
	ldmfd	SP!, {r0-r10,FP,PC}		; return to mainloop

	
;=======
;data 
;=======

InputFileName: 		.asciz 	"A4a.txt"
OutputFileName: 	.asciz 	"Output.txt"
Return:			.asciz	"\n"
Tab:			.asciz	"\t"
Welcome:		.asciz  "This is the main program for Assignment 4 question 1 for C. Xiao #0334947\nInput file A4a.txt has been opened.\n\n"
Msg1:			.asciz	"\nMatrix 1:\n"
Msg2:			.asciz	"\nmultiplied by Matrix 2:\n"
Msg3:			.asciz	"\nresults in:\n"
MsgBig:			.asciz  "\nSize is too big - unacceptable\n"
MsgZero:		.asciz 	"\nSize = 0 is unacceptable\n"
MsgEnd:			.asciz	"\nEnd of file - programs ends.\n"
			.data
			.align
MaxA:			.skip	100	;store at most 5*5 matrix
MaxB:			.skip	100	;store at most 5*5 matrix
MaxResult:		.skip	100	;store at most 5*5 matrix
			
InFileHandle:		.word 	0	;store input file handle
OutFileHandle:	 	.word 	0	;store output file handle
Size:			.word	0	;store size of matrix

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?