Solution to UA1
;Program UA1
;This program implements the following initialisations ; a=5
; if(x<10)
Copyright By PowCoder代写 加微信 powcoder
;if (x<10)
R1,10[R0] R2,x{R0] R3,R2,R1 R3,ELSE[R0]
R1,3[R0] R1,y[R0] ENDIF{R0}
;set constant 5 ;assign 5 to a
;set constant 10 ;retrieve x
;If NOT (x < 10) ;go to else
;set constant 3 ;assign 3 to y ;exit IF
;set constant 4 ;assign 4 to z
;set constant 6 ;assign 6 to b ;Finish
;if x < 10 set y = LEA
STORE JUMP
;if x >=10 set z=4 ELSE LEA
;Using statement by statement style here let¡¯s use R1 for all initialisations. If part of larger program, register variable might be better.
;Data Area. Reserve space for variables used but all except x are initialised by the code
Systems and Networks
;only variable input to this fragment
6 Control Structures 13
Solution to UA2
;Program sumsum
;This program doubles sum from initial value 1 until it exceeds limit. ;
; limit = 300
; while (sum < limit)
; { sum = sum + sum}
;Assignment to registers as follows:
; R1 = sum
; R2 = limit
; R3 for temporary boolean
; Initialise registers
LOAD R1,sum[R0]
LOAD R2,limit[R0] ;Load value of limit
;While loop
WHILE CMPLT
JUMPF R3,OUT[R0]
ADD R1,R1,R1 JUMP WHILE[R0]
;Exit loop and store result
;If sum >=limit
;exit loop
;sum = 2 x sum
;Store result
OUT STORE TRAP
R1,sum[R0]
sum DATA 1 limit DATA 300
Systems and Networks
6 Control Structures 14
;Load initial value of sum
;Program: factorial.
;This program computes n! (factorial).
; HLL algorithm
for (i = 1; i<=n; i++)
{ x = x * i; }
;Assignment to registers as follows:
; R1 = constant 1
; R5 for temporary boolean
; Initialise registers
; For loop follows FORLOOP
LEA LEA LOAD LEA
CMPGT JUMPT MUL ADD JUMP
R1,1[R0] R2,1[R0] R3,n[R0] R4,1[R0]
R5,R2,R3 R5,OUT[R0] R4,R4,R2 R2,R2,R1 FORLOOP[R0]
R4,x[R0] R0,R0,R0
;R1 = 1 (constant) ;i=1
; EXIT Loop ;x = x*i
;i = i + 1 ;Loop again
; Store x ; Finish
; Exit Loop and store result.
OUT STORE TRAP
;Data Area
n DATA x DATA
Systems and Networks
6 Control Structures 15
Solution to UA3
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com