EE251: Tuesday September 5 Shift/Rotate Instructions Bitwise logic and Saturating Instructions A Few Math Programming Examples ARM Assembly Language and Assembler Assembly Process Assembly Structure Assembler Directives Reading: Chapters 3 and 7 Lab #2 finishes this week. Report due next week. Lab #3 starts next week. Online now. Note Lab #3 prework! Homework #1 due Thursday 4:30 pm in box in BC Infill. Lecture #5 1
But First, look at LDR Pseudo Instruction 18: LDR R3,=0x87654321 0x00000008 4B07 LDR r3,[pc,#28] 19: MOV R0, #0... 29: done B done 0x00000024 E7FE B 0x00000024 0x00000026 0000 DCW 0x0000 0x00000028 4321 DCW 0x4321 0x0000002A 8765 DCW 0x8765 Lecture #5 2
Shift and Rotate Instructions Multiple bit positions with single instruction Logical Shift Left (LSL) Arithmetic Shift Right (ASR) Logical Shift Right (LSR) Rotate Right (ROR) Rotate Right Extended (RRX) Lecture #5 3
Shift - Rotate Instruction Examples We are assuming registers are 8 bits wide for easier examples. They are actually 32 bits wide. Examples: LSL r1, r0, #3 Assume [r0] = 0x0F = 0b00001111 = 15 r1 = 0b01111000 = 0x78 = 120 = 15*2 3 LSR r3, r4, #2 Assume [r4]=0xf0=0b11110000=240 r3 = 0b00111100 = 0x3C = 60 = 240 2 2 ASR r3, r4, #2 Assume [r4]=0xf0=0b11110000= -16 r3 = 0b11111100 = 0xFC = -4 = -16 2 2 Signed numbers cause different results for LSR vs. ASR! Other examples? Lecture #5 4
Using Barrel Shifter in Arithmetic The second operand of ALU has a special hardware called Barrel shifter which allows more powerful instruction when shifting is combined with math operations Example: ADD r1, r0, r0, LSL #3 r1 = r0 + r0 << 3 = r0 + 8 r0 = 9 r0 Note: << means shift left and >> means shift right Lecture #5 5
Barrel Shift Examples Examples: ADD r1, r0, r0, LSL #2 r1 = r0 + r0 << 2 = r0 + 4 r0 ADD r1, r0, r0, LSR #3 r1 = r0 + r0 >> 3 = r0 + r0/8 (unsigned) ADD r1, r0, r0, ASR #3 r1 = r0 + r0 >> 3 = r0 + r0/8 (signed) Use Barrel shifter for better performance: compute 9*r0 ADD r1, r0, r0, LSL #3 vs. MOV r2, #9 r2 = 9 MUL r1, r0, r2 r1 = r0*9 Lecture #5 6
Bitwise Logic AND {Rd,} Rn, Op2 Bitwise logic AND. Rd Rn & Op2 ORR {Rd,} Rn, Op2 Bitwise logic OR. Rd Rn Op2 EOR {Rd,} Rn, Op2 Bitwise logic exclusive OR. Rd Rn EOR Op2 ORN {Rd,} Rn, Op2 Bitwise logic NOT OR. Rd Rn (NOT Op2) BIC {Rd,} Rn, Op2 Bit clear. Rd Rn & NOT Op2 BFC Rd, #lsb, #width Bit field clear. Rd[(width+lsb 1):lsb] 0 BFI Rd, Rn, #lsb, #width Bit field insert. Rd[(width+lsb 1):lsb] Rn[(width-1):0] MVN Rd, Op2 Move NOT, logically negate all bits. Rd 0xFFFFFFFF EOR Op2 AND, ORR, EOR behavior is straightforward. See examples Section 4.6 of text. Other examples (showing just 8 bits of the 32 bit registers): ORN R2, R1, R0 R1=10101010 and R0=11001100, not(r0)=00110011 and 10111011 R2 BIC R2 R1 R2=10101010 and R1=11001100, not(r1)=00110011 and 00100010 R2 Lecture #5 7
Example: BFC and BFI Bit Field Clear (BFC) and Bit Field Insert (BFI). Syntax BFC Rd, #lsb, #width BFI Rd, Rn, #lsb, #width Examples BFC R4, #8, #12 Clear bit 8 to bit 19 (12 bits) of R4 to 0 BFI R9, R2, #8, #12 Replace bit 8 to bit 19 (12 bits) of R9 with bit 0 to bit 11 from R2. Lecture #5 8
Examples: Set or Clear bit Set a bit: ORR R0, #1 << n Sets n th bit of R0 Clear a bit: BIC R0, #1 << n Clears nth bit of R0 More Examples? Lecture #5 9
Example Program to do really simple math Write a program to add the word values starting at memory locations at 0x2000.0800, 0x2000.0804, and 0x2000.0808, and save the result starting at 0x2000.080C. Solution: Step 1: Address of first number R0 Step 2: First number into R1 [R0] R1 Step 3: Add second number to first number into R1 Step 4: Add third number to second number into R1 Step 5: Store result at destination address Step 6: End the program ADR R0, 0x20000800 First operand address in R0 LDR R1, [R0], #4 First Operand in R1 & increment R0 ADD R1, [R0], #4 Add second operand & increment R0 ADD R1, [R0], #4 Add third operand & increment R0 STR R1, [R0] Store sum Done BR Done Finished. Infinite Loop (standard) END Does this program do unsigned or signed arithmetic? What if we wanted to save result starting at 0x2000.081C? Lecture #5 10
Second Example Program Write a program to find the inner product of two 2-element vectors. The first vector begins at 0x2000.0400, and the second begins at 0x2000.0600. Store the result at address 0x2000.0700. Inner product is defined as IP=sum_for_all_i( x(i)*y(i)). Note: This is a very common operation in much of engineering (e.g. ECE311) Solution: Inner product of two very short vectors with word-length data ADR R0, 0x20000400 First vector address in R0 ADR R1, 0x20000600 Second vector address in R1 LDR R2, [R0], #4 x(1) in R2 & increment R0 LDR R3, [R1], #4 y(1) in R3 & increment R1 MUL R4, R2, R3 Multiply first elements together Running sum of products is now in R4 LDR R2, [R0] x(2) in R2 LDR R3, [R1] y(2) in R3 MLA R4, R2, R3, R4 Muliply second elements and add to previous ADR R0, 0x20000700 Inner product address STR R4, [R0] Store Inner product Done BR Done Finished. Infinite Loop END Does this program do unsigned or signed arithmetic? How would you write this if each vector had 1000 elements? Lecture #5 11
ARM Assembly Programs Three Components 1. Assembler Directives tell assembler actions to take - Define data and symbols - Reserve and initialize memory locations, ROM and RAM - Allows linking files - Specifies the end of a program - Etc. 2. Assembly Language Instructions ARM executes - Load/Store, Math, Shift/Rotate, Logical, etc. - Plus a few Pseudo-Instructions 3. Comments - Explain the meaning of single or grouped instructions Lecture #5 12
Anatomy of an Assembly Program: AREAs Programs include CODE (ROM) And DATA (RAM) AREAs. Lecture #5 13
Assembly Program: Code and Data Areas Lecture #5 14
Assembly Program: More Details Lecture #5 15
Assembly Directives Text Section 3.6 Directives are not instructions. Instead, they are used to provide key information for assembly. AREA ENTRY ALIGN DCB DCW DCD SPACE FILL EQU RN EXPORT IMPORT INCLUDE/GET PROC ENDP END Make a new block of data or code Declare an entry point where the program execution starts Align data or code to a particular memory boundary Allocate one or more bytes (8 bits) of data Allocate one or more half-words (16 bits) of data Allocate one or more words (32 bits) of data Allocate a zeroed block of memory with a particular size Allocate a block of memory and fill with a given value. Give a symbol name to a numeric constant Give a symbol name to a register Declare a symbol and make it referable by other source files Provide a symbol defined outside the current source file Include a separate source file within the current source file Declare the start of a procedure Designate the end of a procedure Designate the end of a source file Key directives are in BOLD above. Lecture #5 16
AREA Directive Details Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The AREA directive indicates to the assembler the start of a new data or code section. Areas are the basic independent and indivisible unit processed by the linker. Each area is identified by a name. Areas within the same source file cannot share the same name. An assembly program must have at least one code area. A code area can only be read (read-only). A data area may be read from and written to (read-write). Lecture #5 17
END Directive Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The END directive indicates the end of a source file. Any lines of information after this are ignored by the assembler. Each assembly source file must end with this directive. Lecture #5 18
EXPORT and IMPORT Directives Array AREA mydata, DATA, DCD 1, 2, 3, 4, 5 READWRITE Define a data section Define an array with five integers main AREA mycode, CODE, EXPORT main ENTRY PROC... ENDP END READONLY Define a code section Make main visible to the linker Mark the entrance to the program PROC marks the begin of a subroutine Assembly program starts here. Mark the end of a subroutine Mark the end of a program The EXPORT declares a symbol and makes this symbol visible to the linker. The IMPORT gives the assembler a symbol that is not defined locally in the current assembly file but which must be defined in another file to be used by the linker. Lecture #5 19
Data Allocation Directives Directive Description Memory Space DCB Define Constant Byte Reserve 8-bit values DCW Define Constant Half-word Reserve 16-bit values DCD Define Constant Word Reserve 32-bit values (word=32 bits) DCQ Define Constant Reserve 64-bit values SPACE Defined Zeroed Bytes Reserve a number of zeroed bytes FILL Defined Initialized Bytes Reserve and fill each byte with a value Lecture #5 20
Data Allocation Examples hello AREA DCB mydata, DATA, READWRITE "Hello World!",0 Allocate a string terminated by a zero dollar miles DCB 2,10,0,200 Allocate byte integers ranging from -128 to 255 DCW 100,200,50,0 Allocate integers between 32768 and 65535 p SPACE 255 Allocate 255 bytes of zeroed memory space f FILL 20,0xFF,1 Allocate 20 bytes and set each byte to 0xFF binary octal char DCB 0b01010101 Allocate a byte in binary DCB @73 Allocate a byte in octal DCB A Allocate a byte initialized to ASCII A Lecture #5 21
ALIGN Directive (examples) With ARM, in many cases instructions and data must begin on 2, 4, or 8 byte boundaries. The ALIGN directive instructs the Assembler to cause this to happen. AREA example, CODE, ALIGN = 3 Memory address begins at a multiple of 8 I.e. last 3 bits of address are 0 ADD r0, r1, r2 ADD Instructions start at a multiple of 8 AREA mydata, DATA, ALIGN = 2 Address starts at a multiple of four I.e. last 2 bits of address are 0 a DCB 0xFF The first byte of a 4-byte word ALIGN 4, 3 Align to the last byte (3) of a word (4) b DCB 0x33 Set the fourth byte of a 4-byte word c DCB 0x44 Add a byte to make following data misaligned ALIGN Force the next data to be aligned d DCD 12345 Skip three bytes and store the word Lecture #5 22
INCLUDE Directive main INCLUDE constants.s AREA main, CODE, READONLY EXPORT main ENTRY PROC... ENDP END Load Constant Definitions The INCLUDE (also called GET) directive is to include an assembly source file within another source file. More on this later. It is useful to include constant symbols defined by using EQU and stored in a separate source file. We will soon be using these regularly. Lecture #5 23
Assembly Programming Format See SampleCode.s on ECE251 Lab Code Website Assembly Programming Format: Note: *************************************************************** You are not required to include Student's name sections that are not used in your Date Lab # - Lab title program. For example, you will not SampleCode.s need the INCLUDES section for the Description of the program SampleCode *************************************************************** first several assignments. EQU Directives These directives do not allocate memory *************************************************************** SYMBOL DIRECTIVE VALUE COMMENT SYM_ZERO EQU 0x00 *************************************************************** Directives - This Data Section is part of the code It is in the read only section so values cannot be changed. *************************************************************** LABEL DIRECTIVE VALUE COMMENT AREA odata, DATA, READONLY THUMB MSG1 DCB "My message..." DCB 0x0D New line DCB 0x04 End of message DATA1 DCB 0x00 Useful data *************************************************************** Program section *************************************************************** LABEL DIRECTIVE VALUE COMMENT AREA main main, CODE, READONLY THUMB EXTERN OutStr Reference external subroutine EXPORT your code goes here main Lecture #5 24
Some Simple Coding Examples See EZTEST.s on ECE251 Lab Code Website Very Simple Test Framework Requiring no RAM or ROM Data AREA RESET, READONLY EXPORT Vectors Vectors DCD 0x400 stack pointer for empty stack DCD Reset_Handler reset vector AREA MYCODE, CODE, READONLY EXPORT Reset_Handler Reset_Handler Test Code Goes Here done B done END Lecture #5 25
Some Simple Coding Examples See EZTESTROMRAM.s on ECE251 Lab Code Website EZTESTROMRAM: Very Simple Test Framework LABEL DIRECTIVE VALUE AREA ROM, DATA, READONLY Rom Data goes here LABEL DIRECTIVE VALUE AREA RAM, DATA, READWRITE RAM Data Structures go here AREA RESET, READONLY EXPORT Vectors Vectors DCD 0x400 stack pointer for empty stack DCD Reset_Handler reset vector AREA MYCODE, CODE, READONLY EXPORT Reset_Handler Reset_Handler Test Code Goes Here done B done END Lecture #5 26
Summary Shift/Rotate Instructions Bitwise logic and Saturating Instructions A Few Math Programming Examples ARM Assembly Language and Assembler Bring questions Thursday if you re still confused after reading text, reviewing slides, and studying this! Next Lecture (see Chapter 6 in text): Conditional Branch Instructions Conditional Execution Example flowcharts and examples Programming Practice as time allows Lecture #5 27