ECE 2560 L 11 HLL to Assembler Department
ECE 2560 L 11 -HLL to Assembler Department of Electrical and Computer Engineering The Ohio State University ECE 3561 - Lecture 1 1
HLL to Assembler l Pseudo HLL l HLL structure Their flow chart l HHL code l Corresponding Assembler l ECE 3561 - Lecture 1 2
What is Pseudo HLL l Pseudo HLL is a way of expressing an algorithm or procedure for performing a task. l Very similar to modern High Level programming Languages l Best illustrated with an example ECE 3561 - Lecture 1 3
Pseudo HLL example l Sum l a list of 10 integers 3 7 8 2 4 6 5 5 9 1 l There are several methods l The algorithm here Create sum and initialize to 0 l Add first number l Repeat adding number until done with list l ECE 3561 - Lecture 1 4
Pseudo HLL l Sequence of Actions Do action A l Do action B l …. l Do action Q l ECE 3561 - Lecture 1 5
Decisions l l l Decision structures IF condition THEN action if true ELSE action if false END IF; Example: l l l l Remove front bike wheel TURN Bike over IF wheel-has-quick-release-skewer THEN flip level and remove wheel ELSE use wrench to remove nuts from axle remove wheel END IF ECE 3561 - Lecture 1 6
Examples and demo l Example and demo of decision structures l IF THEN ELSE Set up l Test condition l Branch to after the code l The action code l ECE 3561 - Lecture 1 7
Example Is a > b ? l a and b are memory location labels l. data la. word 0 x 00 FF lb. word 0 x 00 FF l The code if a>b THEN actions l cmp a, b ; computes b-a l but what jump? l after l ECE 3561 - Lecture 1 8
The jumps l Jump l Instructions JC, Jump if carry set JHS, Jump if high or same l The C bit is tested l If set (i. e. 1) then the branch is taken l If clear, the next instruction is executed l JEQ, JZ Jump l The Z bit is tested l l if equal, Jump if zero If set (i. e. 1) then the branch is taken If clear, the next instruction is executed ECE 3561 - Lecture 1 9
The Jumps (2) l JGE l Jump if greater or equal The N and V bits are used – l If N and V are set or reset (both 0 or both 1) the branch is taken l JL l Jump if less The N and V bits are used l If only one of N or V is set then the branch is taken l JMP Jump unconditionally ECE 3561 - Lecture 1 10
Jumps (3) l JN l Jump is negative The N bit is tested l l JNC JLO l Jump if carry not set Jump is lower The carry bit C is tested. l l If N is set (i. e. 1) the branch is taken If C is 0 the branch is taken JNE Jump not equal JNZ Jump if not zero l The Z bit is tested l If Z = 0 the branch is taken ECE 3561 - Lecture 1 11
A test program l Write and compile a test program that sets up two values, uses cmp to compare them, and then runs through the branches. l Have values that are both positive, both negative, and one positive-one negative. ECE 3561 - Lecture 1 12
Adding the ELSE condition l If you have an else condition, set up the branch such that when the condition is not met, you have a label to branch to this code section. l l l elsecond Ifendpt cmp a, b jge elsecond ; THEN condition actions jmp ifendpt ; ELSE condition actions ; following code ECE 3561 - Lecture 1 13
Repeat structures - Do Loops l Finite number of times – DO LOOP l l Set up any values for loop FOR counter = start TO end [STEP x] LOOP actions of loop END FOR; l Example: l l Sum = 0; FOR i = 1 to 10 LOOP Sum = Sum + element(i); END FOR; ECE 3561 - Lecture 1 14
A fix number of times l l l For I in 1 to 10 loop action in loop End loop; Assembler Use a register for the current value of the loop counter l In data area have l stval – the starting value, endval – the ending value l l l . data stval. word 0 x 0001 endval. word 0 x 000 F ; loop 15 times ECE 3561 - Lecture 1 15
The structure l l l tol l l done ; start of loop – setup mov &stval, R 8 ; i to R 8 cmp R 8, &endval jeq done ; actions of loop inc R 8 jmp tol ; code after loop ECE 3561 - Lecture 1 16
Demo Want to do loop 4 times Setup – stval of 1, endval of 4 But only execute the loop 3 times Could you solve this with the type of branch? Difficult l Easy – endval is number of times through the loop + 1 OR l i is one less than the iteration through the loop at compare, i. e. initial value is 0 – move inc instruction to just after the jump l l ECE 3561 - Lecture 1 17
New material - TA l New material starts here ECE 3561 - Lecture 1 18
Repeat structures l Indeterminate number of times – Repeat loop or decision loop – action inside the loop causes condition to occur that ends the loop. l 2 structures One has code that executes at least once l One has code that may or may not execute at all l ECE 3561 - Lecture 1 19
While condition l While loop – may or may not execute code l l l l WHILE condition LOOP SEQUENCE_of_ACTIONS END loop; Example: WHILE not EOF LOOP Read line from file Process line END LOOP; ECE 3561 - Lecture 1 20
Repeat l Repeat Until structure REPEAT l Sequence_of_statements l UNITL condition; l l Example - attempt to read will be done at least once l l l REPEAT Read line from file UNTIL EOF; ECE 3561 - Lecture 1 21
Now what is the assembler? l For each of these Pseudo HLL structures what is the corresponding assembler. l Each will have assumption as to were data to be tested is. ECE 3561 - Lecture 1 22
Straight line code - Sequence l Straight line Pseudo HLL – little modification is needed. l l l A = m*x + b in Pseudo HLL Where are values – say in memory locations labeled by the same name Code becomes mov m, R 8 push x call smult pop R 8 add b, R 8 mov R 8, A ECE 3561 - Lecture 1 23
Decision l Decision structure l IF condition THEN action if true l ELSE action if false l END IF; l Example: IF (A<B) THEN temp=A; A=B; B=temp l END IF; l ECE 3561 - Lecture 1 24
Assembler for example l. A and B are in memory l Will use a register for temp. cmp l jl l mov l noexch l A, B ; B-A is positive if A<B noexch ; jump if B<A A, R 6 B, A R 6, B ECE 3561 - Lecture 1 25
A more efficient coding l Could be made a little more efficient Previous code also exchanges when A=B l cmp B, A ; A-B is negative if A<B l jge noexch ; jump if B<=A l mov A, R 6 l mov B, A l mov R 6, B l noexch l l ECE 3561 - Lecture 1 26
When there is an else l Else is similar to the previous code l l IF (A<B) THEN temp=A; A=B; B=temp; ELSE A = 0; END IF; Much like previous l l l l else after cmp jge mov mov jmp clr B, A else A, R 6 B, A R 6, B after A ; A-B is negative if A<B ; jump if B<=A ECE 3561 - Lecture 1 27
Repeat structures l Finite number of times – DO LOOP l l Set up any values for loop FOR counter = start TO end [STEP x] LOOP actions of loop END FOR; l Example: l l l Sum = 0; FOR i = 1 to 10 LOOP Sum = Sum + element(i); END FOR; How would this example be coded? ECE 3561 - Lecture 1 28
Coding for Do Loop l Where are control values? l l l i – the current index value – use R 9 startval – the starting value for i – memory loc endval – the ending value for i – memory sum – in memory element – label of list of values in memory l l tol l l mov cmp jge mov dec clrc rolc add inc jmp i, R 9 endval, R 9 ; R 9 -endval lpexit R 9, R 8 R 8 #element, R 8 @R 8, sum R 9 tol lpexit ECE 3561 - Lecture 1 29
The repeat and while loops l The coding of the repeat and while loops is very similar. Documents where variables are l Need loop control and testing of loop control l Which branch instruction to use takes some thought l Need to remember what the cmp instruction does!! l ECE 3561 - Lecture 1 30
The while loop example l i=0; flag=TRUE; l WHILE flag LOOP l increment i; l Actions when value i; l IF i = 5 THEN flag = False; END IF; l END LOOP; l Now translate this to assembler ECE 3561 - Lecture 1 31
Assembler for while example l l l rpt done mov cmp jeq inc nop cmp jne mov jmp nop #0, &i #1, &flag ; 1=TRUE &flag, #0 done &i ; code to do actions &i, #5 rpt #0, &flag rpt ; code after loop ECE 3561 - Lecture 1 32
Demo l Demo of loop code. ECE 3561 - Lecture 1 33
- Slides: 33