Skip to content

Instantly share code, notes, and snippets.

@DavidJRobertson
Created February 24, 2015 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidJRobertson/12db7f87c84e55be07d6 to your computer and use it in GitHub Desktop.
Save DavidJRobertson/12db7f87c84e55be07d6 to your computer and use it in GitHub Desktop.
;-----------------------------------------------------------------------
; Sigma16 Program BarChart
;
; CS1Q Systems Exercise 1
; David Robertson 2133246R TU02/LB02
; 18 February 2015
;-----------------------------------------------------------------------
; Status Report
;
; The program works as expected.
;-----------------------------------------------------------------------
; Program Description
;
; This program prints a bar chart showing the values in an array x
; containing a sequence of n integers. There is also an integer limit
; that is used to limit the length of output lines.
;
; There is one line of output for each element of x, consisting of a
; row of asterisks (*). Normally, the number of asterisks in line is
; x[i]. However, if x[i] is negative, then a minus sign is printed
; instead of asterisks. Furthermore, the number of asterisks printed
; must never exceed limit. If x[i] > limit then only limit asterisks
; are printed, and they are followed by a greater-than sign (>) which
; indicates that the output was truncated.
;-----------------------------------------------------------------------
; High Level Algorithm
;
; for (i = 0 to n-1) {
; k = 0
; if (x[i] < 0) {
; out[k] = "-"
; k = k + 1
; } else {
; while (k < x[i] and k < limit) {
; out[k] = "*"
; k = k + 1
; }
; if (k < x[i]) {
; out[k] = ">"
; k = k + 1
; }
; }
; out[k] = "\n"
; k = k + 1
; write out (length = k)
; }
; terminate
;-----------------------------------------------------------------------
; Low Level Algorithm
;
; start: i = 0
; outerloop:
; if !(i < n) goto endouterloop
; k = 0
; outerif:
; if !(x[i] < 0) goto outerifelse
; out[k] = "-"
; k = k + 1
; goto endouterif
; outerifelse:
; innerloop:
; if !(k < x[i]) goto endinnerloop
; if !(k < limit) goto endinnerloop
; out[k] = "*"
; k = k + 1
; goto innerloop
; endinnerloop:
; innerif:
; if !(k < x[i]) goto endinnerif
; out[k] = ">"
; k = k + 1
; endinnerif:
; endouterif:
; out[k] = "\n"
; k = k + 1
; write out (length = k)
; i = i + 1
; goto outerloop
; endouterloop:
; terminate
;-----------------------------------------------------------------------
; Assembly language implementation
start
load R1,limit[R0] ; R1 = limit
load R2,n[R0] ; R2 = n
; i = 0
lea R3,0[R0]
outerloop
; if !(i < n) goto endouterloop
cmplt R5,R3,R2 ; R5 = (i < n)
jumpf R5,endouterloop[R0] ; goto endouterloop if not R5
; k = 0
lea R4,0[R0]
load R6,x[R3] ; R6 = x[i]
outerif
; if !(x[i] < 0) goto outerifelse
cmplt R5,R6,R0 ; R5 = (x[i] < 0)
jumpf R5,outerifelse[R0] ; goto outerifelse if not R5
; out[k] = "-"
load R7,minus[R0] ; R7 = "-"
store R7,out[R4] ; out[k] = R7
; k = k + 1
lea R4,1[R4]
; goto endouterif
jump endouterif[R0]
outerifelse
innerloop
; if !(k < x[i]) goto endinnerloop
cmplt R5,R4,R6 ; R5 = (k < x[i])
jumpf R5,endinnerloop[R0] ; goto endinnerloop if not R5
; if !(k < limit) goto endinnerloop
cmplt R5,R4,R1 ; R5 = (k < limit)
jumpf R5,endinnerloop[R0] ; goto endinnerloop if not R5
; out[k] = "*"
load R7,star[R0] ; R7 = "*"
store R7,out[R4] ; out[k] = R7
; k = k + 1
lea R4,1[R4]
; goto innerloop
jump innerloop[R0]
endinnerloop
innerif
; if !(k < x[i]) goto endinnerif
cmplt R5,R4,R6 ; R5 = (k < x[i])
jumpf R5,endinnerif[R0] ; goto endinnerif if not R5
; out[k] = ">"
load R7,gt[R0] ; R7 = "<"
store R7,out[R4] ; out[k] = R7
; k = k + 1
lea R4,1[R4]
endinnerif
endouterif
; out[k] = "\n"
load R7,newline[R0] ; R7 = "\n"
store R7,out[R4] ; out[k] = R7
; k = k + 1
lea R4,1[R4]
; write out (length = k)
lea R8,2[R0] ; R8 = 2
lea R9,out[R0] ; R9 = out
trap R8,R9,R4 ; output out len=k
; i = i + 1
lea R3,1[R3]
; goto outerloop
jump outerloop[R0]
endouterloop
; terminate
trap R0,R0,R0
;-----------------------------------------------------------------------
; Variables - these initial values may be changed
limit data 10 ; maximum number of * to write
n data 15 ; size of array x
x data 1 ; x[0]
data 2 ; x[1]
data 0 ; x[2]
data 3 ; x[3]
data -2 ; x[4]
data 7 ; x[5]
data 8 ; x[6]
data 9 ; x[7]
data 10 ; x[8]
data 11 ; x[9]
data 12 ; x[10]
data 50 ; x[11]
data 3 ; x[12]
data 2 ; x[13]
data 1 ; x[14]
;-----------------------------------------------------------------------
; Constants - initial values should not be changed
minus data $002d ; code for "-"
gt data $003e ; code for ">"
newline data $000a ; code for newline
star data $002a ; code for "*"
;-----------------------------------------------------------------------
; Output buffer
out data $0020 ; out[0]
data $0020 ; out[1]
data $0020 ; out[2]
data $0020 ; out[3]
data $0020 ; out[4]
data $0020 ; out[5]
data $0020 ; out[6]
data $0020 ; out[7]
data $0020 ; out[8]
data $0020 ; out[9]
data $0020 ; out[10]
data $0020 ; out[11]
data $0020 ; out[12]
data $0020 ; out[13]
data $0020 ; out[14]
data $0020 ; out[15]
data $0020 ; out[16]
data $0020 ; out[17]
data $0020 ; out[18]
data $0020 ; out[19]
data $0020 ; out[20]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment