Sigma16: Array Examples from Section 7
Example: Sum an array Example: Given an array X[0], …, X[n-1], write an assembly
language program, Sum, to compute the sum of the elements.
Copyright By PowCoder代写 加微信 powcoder
Test the program with a 5 element array as follows: {17, 2, 150, -3, 25}
Systems and Networks Sigma16 Examples
Step 1: High level algorithm. A traversal over an array with a known number of elements can be done with a for-loop, thus:
for (i = 0; i
; if (X[i] > max)
; { max=X[i]; }
Systems and Networks
Sigma16 Examples
Solution: Part II
; Usage of the registers:
; R3 = X[i]
; R5 temporary Boolean
Systems and Networks
Sigma16 Examples
load R1,X[R0]
lea R2,1[R0]
lea R4,1[R0]
; max = X[0]
; R4 = 1 (for counter)
Systems and Networks
Sigma16 Examples
Initialisation
loop load R3,x[R2] cmplt R5,R3,R0
jumpt R5,done[R0]
cmpgt R5,R3,R1 jumpf R5,skip[R0] add R1,R3,R0
skip add R2,R2,R4
jump loop[R0]
done store R1,max[R0] trap R0,R0,R0
; R3 = X[i]
; R5 = (X[i] < 0)
; go to done if X[i] < 0
; R5 = (X[i] > max)
; go to skip if X[i] <= max ; max = X[i]
; i++ ;gotoloop
; store result in max ; terminate
Systems and Networks
Sigma16 Examples
; The data area: X DATA 2 DATA 42 DATA 224 DATA 19 DATA 4 DATA -1
max DATA 0
The data area
; indicates end of the positive data
; declare max and initialise to 0
Systems and Networks
Sigma16 Examples
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com