Funky ass character counter:
Code: Select all
;Falco Girgis
;CPE321 Homework
;Given a string:
;number of uppercase letters in r4
;number of lowercase letters in r5
;number of non-alpha on the stack (and r6 at the end)
.org 0
main:
la r30, stack
la r0, string
lar r18, charcount
brl r31, r18 ;run our charcount subroutine
;Pop value from stack and store in r6
ld r6, (r30)
addi r30, r30, -4
stop
charcount:
;initialize labels
lar r28, loop
lar r27, capcheck
lar r26, nonalpha
lar r25, loopend
la r21, a
ld r21, 0(r21)
la r22, z
ld r22, 0(r22)
la r24, A
ld r24, 0(r24)
la r19, Z
ld r19, 0(r19)
;Clear our counters
sub r4, r4, r4 ;Upper case characters
sub r5, r5, r5 ;lower case characters
addi r30, r30, 4 ;adjust stack pointer for push
st r4, (r30) ;Push a 0 onto stack.
addi r18, r0, 0 ;copy string to r18
loop:
ld r20, 0(r18) ;load character
brzr r31, r20 ;break to "out" if character is null
sub r23, r20, r21 ;char - a
brmi r27, r23 ;go to "capcheck" if r23 is negative
sub r23, r22, r20 ;z - char
brmi r26, r23 ;go to "nonalpha" if r23 is negative
addi r5, r5, 1
loopend:
addi r18, r18, 4 ;increase string pointer
br r28 ;return to beginning of the loop
capcheck:
sub r23, r20, r24 ;char - 'A'
brmi r26, r23
sub r23, r19, r20 ;'Z' - char
brmi r26, r23
addi r4, r4, 1 ;add to cap count
br r25
nonalpha:
;pop current number of nonalphas
ld r23, (r30)
addi r30, r30, -4
;add one to counter
addi r23, r23, 1
;push number back.
addi r30, r30, 4 ;adjust stack pointer for push
st r23, (r30) ;assume non-alpha count was in r23. Push it
br r25
.org 500
a: .dc "a"
z: .dc "z"
A: .dc "A"
Z: .dc "Z"
string: .dc "ABCDEFGHabIJKLMN98OPQRSTUVWZYZabcdefghijkAAlmnopqrs98tuvwxyz!?*&*$01ad234BH56789",0
.org 1000
stack:
This was a particular bitch, because we had to write our own printing subroutine to print the two sources and the concatenated string.
Code: Select all
; Falco Girgis
; CPE321 SRC Homework 2
; Strcat subroutine
;r4 returns length of concatenated string.
COSTAT: .equ -3824
COUT: .equ -3820
.org 0
main: lar r0, string1
lar r1, string2
lar r2, string3
lar r13, newline
lar r30, stack
lar r3, endL
st r13, (r30) ;Add newline to stack
addi r30, r30, 4
st r1, (r30) ;Add second string to stack
addi r30, r30, 4
st r13, (r30) ;Add newline to stack
addi r30, r30, 4
st r0, (r30) ;Add first string to stack
addi r30, r30, 4
la r18, print ;Print first string
brl r31, r18
la r18, print ;Print newline
brl r31, r18
la r18, print ;Print second string
brl r31, r18
la r18, print ;Print newline
brl r31, r18
st r3, (r30) ;Put end on the stack
addi r30, r30, 4 ;Increment stack pointer
lar r18, concat
brl r31, r18 ;Branch to concat
endL:
st r13, (r30) ;Add newline to stack
addi r30, r30, 4
st r2, (r30) ;Add dest string to stack
addi r30, r30, 4
la r18, print ;Print dest string
brl r31, r18
la r18, print ;Print newline
brl r31, r18
stop
concat: lar r22, loop1
lar r23, loop1end
lar r26, loop2
lar r27, loop2end
addi r25, r2, 0 ;r25 points to dest
st r0, (r30) ;Push first string onto stack
addi r30, r30, 4
la r18, strlen ;Call strlen
brl r31, r18
addi r18, r0, 0 ;copy string1's location to r18
sub r20, r20, r20 ;set iterator to 0
loop1: sub r21, r4, r20 ;find difference between strlen and iterator
brzr r23, r21
ld r19, 0(r18) ;load char from 1st string
st r19, 0(r25) ;store char in new string
addi r25, r25, 4 ;increase dest pointer
addi r18, r18, 4 ;increase source pointer
addi r20, r20, 1 ;increase iterator
br r22
loop1end:
st r1, (r30) ;Push second string onto stack
addi r30, r30, 4
la r18, strlen ;Call strlen
brl r31, r18
addi r18, r1, 0 ;copy string2's location to r18
sub r20, r20, r20 ;set iterator to 0
loop2: sub r21, r4, r20 ;find difference between strlen and iterator
brzr r27, r21
ld r19, 0(r18) ;load char from 2nd string
st r19, 0(r25) ;store char in new string
addi r25, r25, 4 ;increase dest pointer
addi r18, r18, 4 ;increase source pointer
addi r20, r20, 1 ;increase iterator
br r26
loop2end: st r2, (r30) ;Push destination string onto stack
addi r30, r30, 4
la r18, strlen ;Call strlen
brl r31, r18
st r25, 0 ;append a null character to the end.
addi r30, r30, -4 ;decrement stack pointer
ld r31, (r30) ;load original return address
br r31 ;return to main
;==============String Length==========
strlen: lar r29, Out
lar r28, Loop
addi r30, r30, -4 ;Decrement stack pointer
ld r17, 0(r30) ;Pop
addi r18, r17, 0 ;Copying r17 to r18
sub r19, r19, r19 ;counter = 0
Loop: ld r20, 0(r18) ;load a character
brzr r29, r20 ;if(char==0) goto OUT
addi r19, r19, 1 ;add to counter
addi r18, r18, 4 ;increase character pointer
br r28 ;do another loop iteration
Out: addi r4, r19, 0 ;store result in r4
br r31 ;return
; ===============Print================
print: lar r5, Wait
lar r6, End2
lar r7, Loop3
addi r30, r30, -4
ld r8, (r30)
addi r9, r8, 0
Loop3: ld r10, 0(r9)
brzr r6, r10
Wait: ld r11, COSTAT
brpl r5, r11
st r10, COUT
addi r9, r9, 4
br r7
End2: br r31
;===================================
.org 1500
newline: .dc 0x0a, 0x0d, 0
string1: .dc "Carry on my wayward son. ", 0
string2: .dc "There will be peace when you are done.", 0
string3: .dc "", 0
.org 3000
stack: