;EE315Ex02.s ; this is an EQU directive. It is for using labels instead of ; actual hexadecimal memory locatons. ; THUMB mean we are going to be using THUMB type ARM language. It is the language ; we learn in this course THUMB ; AREA reserves a memory location for us. ; "Processor please reserve a memory location for me. It will be a data memory, ; i.e. I will put data in it (no code). ; And make a data alignmen for 2^2=4 bytes. ; i.e. write my data starting from an adress location which is factor of 4. AREA |.data|, DATA, READWRITE, ALIGN=2 ; Create a global variable M. anybody, including the subroutines can reach it. ; M will point the starting address of this memory location. ; and reserve 4 bytes startting from M. EXPORT Mem [DATA,SIZE=40]; ; now put zeros on these 40 bytes startng from M Mem SPACE 40 ; reserve another AREA for me, in the CODE space (i.e. Instruction memory) ; so what I will write after this point will be instructions, not data ; make it read only so that no body writes over it. ; make data alignment of 2^2=4. Start wrting my code from a memory location, ; which is a factor of 4 AREA |.text|, CODE, READONLY, ALIGN=2 ; make Start label, which is the beginning memory location of my instructions. ; so that I may call it from another file, or subroutine, (again make it global) EXPORT Start Start LDR R2,=Mem ; now let's write an 10x16bit array to Mem ; the array is [101 98 85 91 121 103 93 84 86 102] LDR R0,=101 STRH R0,[R2] LDR R0,=98 STRH R0,[R2,#2] LDR R0,=85 STRH R0,[R2,#4] LDR R0,=91 STRH R0,[R2,#6] LDR R0,=121 STRH R0,[R2,#8] LDR R0,=103 STRH R0,[R2,#10] LDR R0,=93 STRH R0,[R2,#12] LDR R0,=84 STRH R0,[R2,#14] LDR R0,=86 STRH R0,[R2,#16] LDR R0,=102 STRH R0,[R2,#18] ; check sum of 10 elements starting from Mem at R0; ; the result should be 0x3C4 (964) BL SumMem ; check the sum of squares of 10 elements of Mem at R0 ; the results is 0X16f72 (94066) BL SumSquaresMem theend B theend ; this is the end of the main code ;We have the subroutine here SumMem ; let's first calcaulte the mean LDR R2,=Mem MOV R0,#0 ;initialise R0 to 0 MOV R3,#0 ;init the counter MOV R7,#10 ; how many elements loop1 ; calcaulte the mean LSL R3,#1 LDRH R1,[R2,R3] ADD R0,R1 LSR R3,#1 ADD R3,#1 CMP R3,R7 BNE loop1 BX LR ;We have the subroutine here SumSquaresMem ; let's first calcaulte the mean LDR R2,=Mem MOV R0,#0 ;initialise R0 to 0 MOV R3,#0 ;init the counter MOV R7,#10 ; how many elements loop2 ; calcaulte the mean LSL R3,#1 LDRH R1,[R2,R3] MUL R1,R1 ADD R0,R1 LSR R3,#1 ADD R3,#1 CMP R3,R7 BNE loop2 BX LR ALIGN ; This is a directive, not an instruction. ; I wont write any ore code or data. but if I decide to write, you better stay aligned. END ; we are really done this time.