Assembler: Basics Alberto Bosio bosio@lirmm.fr Univeristé de Montpellier October 20, 2017
Assembler Program Template. t e x t / S t a r t o f the program code s e c t i o n /.data / V a r i a b l e s / x :. b y t e 0. g l o b a l main / d e c l a r e s the main i d e n t i f i e r / main : / Address o f the main f u n c t i o n / / Program code would go h e r e / bx l r / Return to the c a l l e r /. e n d / End o f the program / 2
Assembler Program Template The.text directive indicates the start of a section of the assembly program that contains code. The.data directive indicates the start of a section of the assembly program that contains global variables. The.global main line specifies that main is a globally visible identifier, which can be referred to from outside this file. This is necessary, because main is the program entry point, and the corresponding function will be invoked from outside the program! main: bx lr is the first (and only) instruction of this program. The.end directive specifies the end of the program. 3
Assembly Language Instructions can be divided in three different sets: Arithmetic-Logic instructions perform mathematical operations on data: these can be arithmetic (sum, subtraction, multiplication), logic (boolean operations), or relational (comparison of two values). Branch instructions change the control flow, by modifying the value of the Program Counter (R15). They are needed to implement conditional statements, loops, and function calls. Load/Store instructions move data to and from the main memory. Since all other operations only work with immediate constant values (encoded in the instruction itself) or with values from the registers, the load/store instructions are necessary to deal with all but the smallest data sets. 4
Arithmetic-Logic Instructions Syntax ADD Rd Rn Rm/#imm SUB Rd Rn Rm/#imm MUL Rd Rn Rm AND Rd Rn Rm/#imm EOR Rd Rn Rm/#imm ORR Rd Rn Rm/#imm MVN Rd Rm/#imm CMP Rn Rm/#imm MOV Rd Rm/#imm Semantics Rd = Rn + Rm/#imm Rd = Rn - Rm/#imm Rd = (Rn * Rm/#imm)%2 32 (i.e., truncated at 32 bits) Rd = Rn & Rm/#imm (bitwise and) Rd = Rn ^Rm/#imm (bitwise exclusive or) Rd = Rn Rm/#imm (bitwise or) Rd = Rm/#imm (bitwise negation) Status <- comparison (<, >, ==) of Rn and Rm /#imm Rd = Rm/#imm where Rd, Rn and Rm are any three registers, and #imm are 8-bit immediates 5
Arithmetic-Logic Instructions: Example C program i n t i =0, j =2,k=4; i=k j ; / B i t w i s e or / ASM program mov r4 #0 mov r5 #2 mov r6 #4 o r r r4 r5 r6 / i=k j ; / 6
Branch Instructions Syntax B label BEQ label BNE label BGT label BGE label BLT label BLE label BL label BX Rd Semantics jump to label (unconditional) j,jump to label if previously compared values were equal jump to label if previously compared values were different jump to label if previously compared Rn >Rm/#imm jump to label if previously compared Rn >= Rm/#imm jump to label if previously compared Rn <Rm/#imm jump to label if previously compared Rn <= Rm/#imm function call (label is the function name/entry point) return from function (always as BX lr) 7
Branch Instructions B label is the basic branch. Control is changed to run the instruction labeled with label next. BL label performs a function call, which means it jumps as in B label, but also saves the value of the Program Counter in the Link Register Return from function is performed by BX LR. It is almost the same as MOV PC LR, but the PC should not be manipulated explicitly by programmer, unless there is a very good reason to do so. 8
Translating Conditional Statements C program i n t x, y ;... i f ( x >0) { y =1; } e l s e { y =2; } return y ; Assuming x and y are stored in R4 and R5 respectively 9
Translating Conditional Statements ASM program e l s e : end : cmp r4,#0 b l e e l s e mov r5, #1 b end mov r5, #2 mov r0, r5 bx l r 10
Translating While and Do-While Statements C program i n t x, y ;... y =1; while ( x >0) { y =x ; x ; } return y ; 11
Translating While and Do-While Statements ASM program loop : end : mov r5 #1 cmp r4 #0 b l e end mul r6, r5, r4 mov r5, r6 sub r4, r4, #1 b loop mov r0, bx l r r5 12
Translating While and Do-While Statements C program i n t x, y ;... y =1; do { y =x ; x ; } while ( x >0); return y ; 13
Translating While and Do-While Statements ASM program loop : end : mov r5 #1 mul r6, r5, r4 mov r5, r6 sub r4, r4, #1 cmp r4 #0 bgt loop mov r0, bx l r r5 14
Translating While and Do-While Statements What about for loop? i n t x, y ;... f o r ( y =1; x >0; x ) y =x ; return y ; 15
Load/Store Instructions LDR and STR: op{type} Rt, [Rn {, #offset}] ; immediate offset op{type} Rt, [Rn, Rm {, LSL #n}] ; register offset Where: op is either LDR (load register) or STR (store register) type is one of the following: B: Unsigned byte, zero extends to 32 bits on loads SB: Signed byte, sign extends to 32 bits (LDR only) H: Unsigned halfword, zero extends to 32 bits on loads SH: Signed halfword, sign extends to 32 bits (LDR only) Rt is the register to load or store Rn is the register on which the memory address is based offset is an offset from Rn. If offset is omitted, the address is the contents of Rn Rtm is a register containing a value to be used as the offset LSL #n is an optional shift, with n in the range 0 to 3 16
Global Variables.data Directives.int,.byte,.string respectively reserve memory for a 32-bit integer, 8-bit integer and a a zero-terminated string (the size of the string is one byte per character, plus one for the terminating zero).data x :. i n t 42 s :. s t r i n g " h e l l o! " b :. b y t e h, e, l, l, o,!,0. t e x t l d r r0, =s l d r r1, =b 17
Global Variables Exercice Write a program able to calculate the string length. 18
Global Variables Array. equ N, 10 / c o n t s t a n t N /. data v e c t o r :. space N, 0 x :. i n t 0, 0, 0, 0... 19
Global Variables Bidimensional Array (Matrix) 5 Row _ 0 0 1 2 3 4 1 5 6 7 8 9 3 2 1 0 11 12 13 1 4 _ _ Column 0 1 2 3 4 20
Global Variables Bidimensional Array (Matrix). equ Row, 3. equ Column, 5. equ N, Row Column. data m a t r i x :. space N, 0... m a t r i x [ i ] [ j ] = a r r a y [ i Column + j ] 21
Global Variables Exercice Write a program able to visit the matrix diagonal (NxN). 22