程序代写代做代考 algorithm Microsoft PowerPoint – SN-2016-Sec06.pptx

Microsoft PowerPoint – SN-2016-Sec06.pptx

Solution to UA1
;Program UA1
;This program implements the following initialisations
; a = 5
; if(x<10) ; {y=3} ; else ; {z=4} ; b = 6 ;Using statement by statement style here let’s use R1 for all initialisations. If part of larger program, register variable might be better. ;a = 5 LEA R1,5[R0] ;set constant 5 STORE R1,a[R0] ;assign 5 to a ;if (x<10) LEA R1,10[R0] ;set constant 10 LOAD R2,x{R0] ;retrieve x CMPLT R3,R1,R2 ;If NOT (x < 10) JUMPF R3,ELSE[R0] ;go to else ;if x < 10 set y = 3 LEA R1,3[R0] ;set constant 3 STORE R1,y[R0] ;assign 3 to y JUMP ENDIF{R0} ;exit IF ;if x >=10 set z=4
ELSE LEA R1,4[R0] ;set constant 4

STORE R1,z[R0] ;assign 4 to z

;b=6
ENDIF LEA R1,6[R0] ;set constant 6

STORE R1,b[R0] ;assign 6 to b
TRAP R0,R0,R0 ;Finish

;Data Area. Reserve space for variables used but all except x are initialised by the code
a DATA 0
b DATA 0
x DATA 12 ;only variable input to this fragment
y DATA 0
z DATA 0

Systems and Networks 6 Control Structures 17

Solution to UA2
;Program sumsum
;This program finds …
;
; sum = 1
; 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 initial value of sum LOAD R2,limit[R0] ;Load value of limit ;While loop WHILE CMPLT R3,R1,R2 ;If sum >=limit
JUMPF R3,OUT[R0] ;exit loop
ADD R1,R1,R1 ;sum = 2 x sum
JUMP WHILE[R0]

;Exit loop and store result

OUT STORE R1,sum[R0] ;Store result
TRAP R0,R0,R0 ;Finish

Data Area

sum DATA 1
limit DATA 300

Systems and Networks 6 Control Structures 18

Solution to UA3
;Program to …

; HLL algorithm
; x = 1;
; for (i = 1; i<=n; i++) ; { x = x * i; } ;Assignment to registers as follows: ; R1 = constant 1 ; R2 = i ; R3 = n ; R4 = x ; R5 for temporary boolean ; Initialise registers LEA R1,1[R0] ;R1 = 1 (constant) LEA R2,1[R0] ;i = 1 LOAD R3,n[R0] ;R3 = n LEA R4,1[R0] ;x = 1 ; For loop follows FORLOOP CMPGT R5,R2,R3 ;IF i>n
JUMPT R5,OUT[R0] ; EXIT Loop
MUL R4,R4,R2 ;x = x*i
ADD R2,R2,R1 ;i = i + 1
JUMP FORLOOP[R0] ;Loop again

; Exit Loop and store result.

OUT STORE R4,x[R0] ; Store x
TRAP R0,R0,R0 ; Finish

;Data Area

n DATA 5
x DATA 0

Systems and Networks 6 Control Structures 19