Name:
OVERVIEW
Memory concepts in assembly language:
Memory is byte-addressable
Assembly language programs can read and write from RAM using LDR and STR instructions
Different sections of memory have different access properties, some is read-only (ROM) and some is read-write (RAM)
The technical specifications of each device indicate which memory addresses have different protection levels.
Memory storage can be implemented with different byte ordering. The particular byte convention that is used is known as “endian-ness” and it changes from device to device.
Memory is transferred in a variety of bit-length fields depending on the data-type appropriate for the particular application.
Common bit-length fields are byte (8-bit), half word (16-bit), word (32-bit) , double word (64-bit).
More about LDR, STR,
A LDR pseudo-instruction can store any 32-bit value in a register. For example:
LDR R0, = 0xFFF43209 ; Load immediate
But you can also load a register with a 32-bit value stored in memory. You have to have a memory location and a memory address
First, assume that memory address 0x0000003C has content 0xFFFFFFF73
First load the address into a register
LDR R1, =0x0000003C ; Load value (address) into R0
LDR R2, [R1] ; Load value at 0x000003C inot R2. R2 stores 0xFFFFFF73
You can change the contents of memory similarly using the STR instruction. For example
MOV R2, #2
LDR R1, =0x0000003C ; Load value (address) into R0
STR R2, [R1] ; Load value at 0x000003C inot R2. R2 stores 0xFFFFFF73
Will put the value 2 in the memory location 0x0000003C, overwriting any existing content.
Stacks Concepts:
Stacks are simply areas of memory that are accessed in a way that enforces a First In Last Out concept.
R13 is used to point to a memory address and is called the stack pointer (SP).
A PUSH {R0} is a convenient way to implement two instructions memory:
SUB R13, R13, #4 ; SP <- SP -4 ; Decrement stack pointer LDR R0, [R13] ; [R13] <- R0 ; Copy contents in R0 to memory A POP {R0} is a convenient way to access values stored in memory: STR R0, [R13] ; R0 <- [R13] ; Get value from memory and place in R0 ADD R13, R13, #4 ; SP <- SP + 4 ; Increment stack pointer A stack pointer keeps track of the RAM memory address where values are temporarily stored. Moving data to and from the stack (in RAM) is done with pseudo-instructions PUSH and POP, respectively. R13 (SP) is the stack pointer. Before this method can be used A special pseudo-instruction, LDR, is used to load the stack location in R13 . For example. LDR SP, =0x40000020 The LR value is stored at this location in memory by doing this: PUSH { LR} And restored with this POP { LR} Nested subroutine calls are calls to a subroutine from within a subroutine. BX and BL are used. A problem arises because the LR to is overwritten by the call to the inner subroutine. Subsequent calls to BX LR do not work as expected. The solution is to save the LR to the stack before overwriting it. So to return properly from a nested subroutine, the LR be saved before each call and restored before returning. While this can be done by saving the LR (R14) as you might any register by moving it to any other register, the usual method is to use a section of memory in RAM called the stack. More about LDR, STR, ADR, DCD A LDR pseudo-instruction can store any 32-bit value in a register. For example: LDR R0, = 0xFFF43209 ; Load immediate But you can also load a register with a 32-bit value stored in memory. You have to have a memory location and a memory address First, assume that memory address 0x0000003C has content 0xFFFFFFF73 First load the address into a register LDR R1, =0x0000003C ; Load value (address) into R0 LDR R2, [R1] ; Load value at 0x000003C inot R2. R2 stores 0xFFFFFF73 You can change the contents of memory similarly using the STR instruction. For example MOV R2, #2 LDR R1, =0x0000003C ; Load value (address) into R0 STR R2, [R1] ; Load value at 0x000003C inot R2. R2 stores 0xFFFFFF73 Will put the value 2 in the memory location 0x0000003C, overwriting any existing content. The usefulness of this quickly becomes obvious when you learn how to define data and reference by a label using DCW and ADR You can define data in your program like this: mydata DCW 42 You can use ADR to interpret the label as an address and store in a register as follows: ADR R0, mydata LDR R1, [R0] R1 now contains the value 42, stored at the address labeled mydata PROBLEMS 1) Type in and trace the following code and identify the problem. Modify the program so that it works correctly, calling func2 from func1 and returning as expected. And explain what you did to modify the program. What is the SP? What two things changes as a result of a PUSH instruction? What two things get modified as a result of a POP instruction? 2) Type in the following program. Verify the memory address by looking at the disassembly window. Memory Address Label Instruction (Assembly Language Mnemonic) 0x00000000 LDR R13, =0x4000001C 0x00000004 MOV R4, #3 0x00000008 BL func1 0x0000000C SUB R4, R4, #1 0x00000010 B stop 0x00000014 func1 MOV R5, #5 0x00000018 PUSH {LR} 0x0000001C BL func2 0x00000020 SUB R4, R4, #1 0x00000024 POP {LR} 0x00000028 BX LR 0x0000002C func2 MOV R6, #6 0x00000030 BX LR Trace the values that are stored in the PC, LR and SP as you step through the program. (if a register doesn’t change at a step, simply write a dash for convenience.) Step PC LR SP 0 (start) 0x00000000 0x00000000 0x00000000 Look at the contents of memory and write the values found there. You may have to count bytes. MEMORY 4 bytes of data at this address 0x40000018 0x4000001C 0x40000020 Explore Stacks and Memory. What is in memory and registers after the following PUSH/POP sequence at the designated points in the program? Memory Address (in hex) Contents at POINT A Contents at POINT B Contents at POINT C 0x4000000C 0x40000010 0x40000014 0x40000018 0x4000001C 0x40000020 0x40000024 0x40000028 0x4000002C ( For convenience, you may show R4-R8 as single digit decimal values rather than 8-byte hex.) Register Contents at POINT A Contents at POINT B Contents at POINT C R4 R5 R6 R7 R13 Explore LDR and STR and Memory. Show what is stored in the indicated registers and in each memory location after the following code is executed. Memory Address (in hex) Contents (in hex) 0x40000000 0x40000004 0x40000008 0x4000000C Register Contents Register Contents R0 R1 R2 R3 R4 R5 [Type text] [Type text] [Type text] CS 2240 / Fall 2020 / Lab 4– Nested subroutines, stacks pointer PAGE \* MERGEFORMAT 6-A AREA TracePC1, CODE, READONLY ENTRY main MOV R4, #4 BL func1 MOV R5, #5 stop B stop func1 MOV R6, #6 BL func2 BX LR func2 MOV R7, #7 BX LR END LDR R13, =0x4000001C MOV R4, #4 MOV R5, #5 MOV R6, #6 MOV R7, #7 PUSH {R4} ----- POINT A ------------------------------ POP {R7} PUSH { R6, R4, R5} ----- POINT B ------------------------------ POP { R5, R6, R4} PUSH {R4} PUSH{ R5} ----- POINT C ------------------------------ LDR R0, =0x40000004 ; Set R0 with memory addresses MOV R2, #-4 ; Set R2 with a value STR R2 , [R0] ; Store R2 value in memory LDR R4, [R0] ; Copy value in memory to R4 MOV R3, 0xBB LDR R1 = 0x4000000C STR R3, [R1 ] LDR R5, [R1 ]