Skip to content

Instantly share code, notes, and snippets.

/A4.S

Created November 6, 2017 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3cf1b35e92a5fea18005183eaaa59b2d to your computer and use it in GitHub Desktop.
Save anonymous/3cf1b35e92a5fea18005183eaaa59b2d to your computer and use it in GitHub Desktop.
; ========================================================================
; Name: Artem Golovin
; Email: agolo124@mtroyal.ca
; Course: COMP2655-001
; Due Date: November 5th
; Assignment: 4
; Instructor: ppospisil
; Source: A4.S
; Purpose: To gain experience with branching and to implement
; "snaking" column representation for sorted data
; Details: Linux "ls" command, by default displays output in columns
; whose format is snaking columns. The information is listed
; alphabetically in columns instead of rows. If the number of
; items is not evenly divisible by the number of columns,
; extra items are distributed in the columns from left to right
;
; Algorithm works this way:
; - Calculate longest string in the file
; - Read the file n times, where n is number of rows (incl. extra row)
; - Read word from the file
; - Skip first words if reading file not for the first time
; - Print the word
; - After new line is reached, skip words in file:
; - If there's an extra row:
; - Skip rows - 1 words for m columns that contain extra row
; - Skip rows - 2 words for the rest of the columns
; - Else: Skip rows - 1 words
; - Calculate # of whitespaces
; - Column Width - current string size
;
; Assumptions & Limitations:
; It is assumed that provided files are already sorted
; alphabetically
;
; Register Table:
; register usage
; ==========================================================
; d0 Mainly used to store character that was read
; from file and to output a charcter to screen
; d1 Used to store current file size. Represents
; EOF when reached 0
; d2
; d3
; d4
; d5
; d6
; d7
;
;
;
;=========================================================================
xref FileOpen
xref FileCharRead
xref FileReset
xref FileClose
xref ScrnCharWrite
xref Exit
CR equ 13
LF equ 10
NULL equ 0
WIDTH equ 80
SPACE equ 32
start:
clr.l d3
jsr FileOpen
tst d0
bne invalidFile
tst d1
beq emptyFile
initFileRead:
jsr FileCharRead
sub.w #1,d1
tst.w d1
beq doneInitRead
cmp.b #CR,d0
beq initFileRead ; get back to read LF
cmp.b #LF,d0
beq countWords ; count words if CRLF reached
add.w #1,d3
bra initFileRead
countWords:
add.w #1,d2
cmp.w d3,d4
blt longestWord
clr.l d3
bra initFileRead ; continue reading
longestWord:
move.w d3,d4
clr.l d3
bra initFileRead
doneInitRead:
nop
move.l d4,d3
clr.l d4
add.w #1,d2 ; add missing word to total word couter
tst.w d2 ; check if file is not empty
beq emptyFile
calculate:
clr.l d0
add.w #1,d3 ; column width
swap d2
move.w d3,d2 ; words / col width
clr.l d3
move.w #WIDTH,d7
divu.w d2,d7
move.w d7,d3 ; num of cols
tst d3
beq singleCol
bne continueCalc
singleCol:
move.w #1,d3
bra continueCalc
continueCalc:
clr.l d7
swap d2
move.w d2,d7
divu.w d3,d7 ; get rows and extra rows
move.l d7,d4
swap d3
move.w d4,d3 ; store rows with cols
clr.w d4
swap d4
tst.w d4
bne addExtraRow
beq startOutput
addExtraRow:
addq.w #1,d3
bra startOutput
startOutput:
clr.l d7
swap d4
move.w d3,d4
mainLoop:
cmp.w d3,d5 ; currentRow(d5) < rows(d3.w)
bgt doneProgram
jsr FileOpen
sub.w #1,d4
swap d4
; skip over #currentRow files
clr.l d6
clr.l d0 ; wordCounter
tst d5
beq readFileLoop
skipWordsOuterLoop:
cmp.w d5,d7
bge readFileLoop
skipWordsInnerLoop:
jsr FileCharRead
sub.w #1,d1
cmp.b #CR,d0
beq skipWordsInnerLoop
cmp #LF,d0
beq endSkipWordsOuterLoop
bra skipWordsInnerLoop
endSkipWordsOuterLoop:
add.w #1,d7 ; currentRow++
bra skipWordsOuterLoop
readFileLoop:
clr.l d6
move.w d4,d6
swap d6
showWord:
jsr FileCharRead
sub.w #1,d1
cmp.b #CR,d0
beq skipCRLF
jsr ScrnCharWrite
add.w #1,d6
bra showWord
skipCRLF:
jsr FileCharRead
sub.w #1,d1
cmp.b #LF,d0
beq traverseFile
traverseFile:
nop
swap d6
clr.l d0
;move.w d3,d0
;sub.w #1,d0
;tst d6
;bne continueTraverse
;cmp.w d5,d0
;beq endMainLoop
continueTraverse:
tst.w d4
beq skip1word
cmpi.w #0,d6
ble skip2words
bra skip1word
skip2words:
clr.w d7
clr.w d0
move.w d3,d0
sub.w #2,d0
skip2wordsOuter:
cmp.w d0,d7
bge showSpaces
skip2wordsInner:
jsr FileCharRead
sub.w #1,d1
cmp.b #CR,d0
beq skip2wordsInner
cmp #LF,d0
beq skip2wordsDone
bra skip2wordsInner
skip2wordsDone:
move.w d3,d0
sub.w #2,d0
add.w #1,d7
bra skip2wordsOuter
skip1word:
clr.w d7
clr.w d0
move.w d3,d0
sub.w #1,d0
skip1wordOuter:
cmp.w d0,d7
bge showSpaces
skip1wordInner:
jsr FileCharRead
sub.w #1,d1
cmp.b #CR,d0
beq skip1wordInner
cmp #LF,d0
beq skip1wordDone
bra skip1wordInner
skip1wordDone:
move.w d3,d0
sub.w #1,d0
add.w #1,d7
bra skip1wordOuter
showSpaces:
swap d2
clr.w d7
move.w d2,d7
swap d2
swap d6
sub.w d6,d7 ; # of spaces to show
swap d6
sub.w #1,d6
spacesLoop:
tst.w d7
beq readNextWord
move.w #SPACE,d0
jsr ScrnCharWrite
sub.w #1,d7
bra spacesLoop
readNextWord:
clr.l d0
move.w d3,d0
swap d4
tst.w d4
beq doneProgram
swap d4
; tst.w d6
; bne contReadNextWord
; cmp.w d5,d0
; beq endMainLoop
swap d6
clr.w d6
contReadNextWord:
cmp.w #0,d1
ble endMainLoop
bne showWord
endMainLoop:
clr.l d1
add.w #1,d5
move.b #CR,d0
jsr ScrnCharWrite
move.b #LF,d0
jsr ScrnCharWrite
swap d6
move.w d4,d6
swap d6
swap d4
jsr FileClose
bra mainLoop
emptyFile:
clr.l d0
move.l #emptyFileMsg,a0
bra showErrorMessage
invalidFile:
clr.l d0
move.l #invalidFileMsg,a0
bra showErrorMessage
showErrorMessage:
move.b (a0)+,d0
beq doneProgram
jsr ScrnCharWrite
bra showErrorMessage
doneProgram:
jsr Exit
invalidFileMsg:
dc.b "Invalid file/File doesn't exist",CR,LF,NULL
emptyFileMsg:
dc.b "Empty file",CR,LF,NULL
;fetchChar:
;move.w $FFE000,d0
;bmi fetchChar ;Loop while reads a < 0 value
;Read something > 0 => got a character
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment