Page 1 of 2

Assembly Homework

Posted: Thu Oct 23, 2008 10:30 pm
by Falco Girgis
I just put off my CPE321 homework of over a week until the last minute--yet again. I've been scrambling to get it done in the last few hours. Here's the two homework assignments so far. If you're wondering, it's SRC assembly, which pretty much doesn't exist. You aren't going to find the processor, and we're only simulating it in Java applets, rofl.

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:

strcat()
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: 

Re: Assembly Homework

Posted: Thu Oct 23, 2008 11:19 pm
by Icjusty
Ugh, seems like you have to do so much to accomplish so little. I still would like learn some assembly though, because it would definitely hardcore/sweet/badass.

Re: Assembly Homework

Posted: Fri Oct 24, 2008 1:04 pm
by sparda
I tried googling for a SRC processor (I've never heard of it), and I don't exactly get what I want. Google returns results relating to Semiconductor Research Corporation, which is obviously not a processor, but a hunch tells me it deals with this company. Anyway, if I'd have to guess, I'd say the processor relates to the PowerPC architecture because of all the "r" enumerations for registers.

Falco, why don't you tell your god damn archaic professor to upgrade to at least... well who am I to speak, I programmed for the SNES which is pretty darn old.

But at least I can find a 65816 processor! :lol:

Re: Assembly Homework

Posted: Tue Oct 28, 2008 5:50 pm
by ultimatedragoon69
imo an easy way to learn assembly is to start at a level that is very easy to understand. HLA (high level assembly) was actually created to move people into assembly very smoothly. What i would do is learn all the high language features then move your way into pure assembly eventually it will be like second nature. Of course at that level all programming becomes almost unreadable in my opinion.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 1:40 pm
by ultimatedragoon69
well i got an assembly project that seems its going to be a pain. I have to make a sim-city clone in assembly haha off to work.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 1:45 pm
by Falco Girgis
ultimatedragoon69 wrote:well i got an assembly project that seems its going to be a pain. I have to make a sim-city clone in assembly haha off to work.
Christ. :shock: Good luck, man.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 2:16 pm
by ultimatedragoon69
thanks i'm really gonna need it only have a month to get it finished.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 4:31 pm
by rolland
A month? Damn... Well, good luck then. And may the powers of caffeine be with you :mrgreen:

Re: Assembly Homework

Posted: Wed Oct 29, 2008 5:04 pm
by trufun202
ultimatedragoon69 wrote:well i got an assembly project that seems its going to be a pain. I have to make a sim-city clone in assembly haha off to work.
ouch. That's harsh... Hell, one month for a C++ clone of Sim-City would be crazy enough.

Good luck, sir. I look forward to seeing the finished product.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 10:56 pm
by M_D_K
ultimatedragoon69 wrote:well i got an assembly project that seems its going to be a pain. I have to make a sim-city clone in assembly haha off to work.
That dude is going to have pump the caffeine straight into the vains. Best of luck to you on that.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 10:59 pm
by Falco Girgis
Out of curiosity, what the hell major/degree are you seeking? Sounds like Computer Engineering, but it sounds way too hard for an undergrad degree.

Re: Assembly Homework

Posted: Wed Oct 29, 2008 11:17 pm
by ultimatedragoon69
currently working on my BA in computer science.

Re: Assembly Homework

Posted: Tue Nov 04, 2008 6:42 pm
by Falco Girgis
I had to write the Ackerman in SRC assembly:

Code: Select all

; Falco Girgis
; 11/4/08
; CPE 321 Assembly Homework 3
; Ackerman (the beast)

		.org	0
main:		la	r30, stack
		la	r18, m
		ld	r18, (r18)
		addi	r30, r30, 4
		st	r18, (r30)	;Push m onto stack
		la	r18, n
		ld	r18, (r18)
		addi	r30, r30, 4
		st	r18, (r30)	;Push n onto stack
		lar 	r18, ack
		brl	r31, r18	;Break to ack, come back at r31
		ld	r0, (r30)
		addi	r30, r30, -4  ;Pop return value from stack
		stop

ack:		ld 	r19, (r30)	;pop n
		addi 	r30, r30, -4	;adjust stack pointer for pop
		ld 	r20, (r30)	;pop m
		addi 	r30, r30, -4	;adjust stack pointer for pop
		la 	r29, next1	;point r29 to next1
		brnz	r29, r20	;branch to next1 if m isn't 0
		addi	r21, r19, 1	;calc n + 1
		addi 	r30, r30, 4	;adjust stack pointer for push
		st 	r21, (r30)	;return n + 1 on stack
		br 	r31		;branch to where link reg says
next1:		la	r29, next2	;point r29 to next2
		brnz	r29, r19	;branch to next2 if n isn't 0
		addi 	r30, r30, 4	;adjust stack pointer for push
		st 	r31, (r30)	;push link reg
		addi	r22, r20, -1	;calc m - 1
		addi 	r30, r30, 4	;adjust stack pointer for push
		st	r22, (r30)	;push m -1 
		sub	r19, r19, r19	;n = 1
		addi	r19, r19, 1
		addi	r30, r30, 4	;adjust stack pointer for push  ERROR... was "pop" should be "push"
		st 	r19, (r30)	;push n = 1
		la 	r29, ack	;point r29 to ack subroutine
		brl	r31, r29	;branch & link to ack subroutine
		ld	r23, (r30)	;pop Ack(m-1, 1)
		addi 	r30, r30, -4	;adjust stack pointer for pop
		ld	r31, (r30)	;pop link reg
		addi 	r30, r30, -4	;adjust stack pointer for pop
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r23, (r30)	;push Ack(m-1, 1)
		br	r31		;branch to where link reg says
next2:		addi 	r30, r30, 4	;adjust stack pointer for push
		st	r31, (r30)	;push link reg
		addi	r22, r20, -1	;calc m - 1
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r22, (r30)	;push m - 1
			;calc m
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r20, (r30)	;push m
		addi	r24, r19, -1	;calc n - 1
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r24, (r30)	;push n - 1
		la	r18, ack	;point r18 to ack subroutine
		brl	r31, r18	;branch & link to ack subroutine calc Ack(m, n-1)
		ld	r26, (r30)	;pop Ack(m, n-1)
		addi	r30, r30, -4	;adjust stack pointer for pop
		ld	r27, (r30)	;pop m-1	
		addi 	r30, r30, -4	;adjust stack pointer for pop
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r27, (r30)	;push m-1
		addi	r30, r30, 4	;adjust stack pointer for push
		st	r26, (r30)	;push Ack(m, n-1)
		la	r18, ack	;point r18 to ack subroutine
		brl	r31, r18	;branch & link to ack subroutine calc Ack(m-1, Ack(m, n-1))
		ld	r27, (r30)	;pop Ack(m-1, Ack(m, n-1))
		addi 	r30, r30, -4	;adjust stack pointer for pop
		ld	r31, (r30)	;pop link reg
		addi 	r30, r30, -4	;adjust stack pointer for pop
		addi 	r30, r30, 4	;adjust stack pointer for push
		st	r27, (r30)	;push result
		br	r31	;branch to where link reg says

m:		.dc 3
n:		.dc 4

		.org 1000
stack:
It takes the bastard around 10 minutes to calculate m = 3, n = 2. I'm trying to do m = 3, n = 4, and it has been going for half an hour. SRC processor == epic fail.

Re: Assembly Homework

Posted: Tue Nov 04, 2008 7:00 pm
by avansc
ultimatedragoon69 wrote:currently working on my BA in computer science.
BA?

Re: Assembly Homework

Posted: Tue Nov 04, 2008 7:06 pm
by avansc
ultimatedragoon69 wrote:imo an easy way to learn assembly is to start at a level that is very easy to understand. HLA (high level assembly) was actually created to move people into assembly very smoothly. What i would do is learn all the high language features then move your way into pure assembly eventually it will be like second nature. Of course at that level all programming becomes almost unreadable in my opinion.
im gonna have to disagree. assembly, at least 8086 intel, is nothing like high level language.
staring straight in assembly will be easier than to go going from high to low.

this is a simple 8086 intel asm program

Code: Select all

;----------------------------------------------------------------
; program: key ***** TASM VERSION *****
; function: echos all the characters pressed on the keyboard, 
;			changes lowercase to uppercase. echos only alphabetic
;			character and the space character. will echo a period
;			and end the program.
;
; to use type key.exe and type any key into the keyboard. end by pressing a period.
;
; Output: all alphabtic characters in uppercase and period ends the program.
;
; owner: Andre van-Schalkwyk
;
; date: 05/29/2004
;
;--------------------
	.model small	; 64k data and 64k code
	.8086			; 8086 instructions
	.stack 256		; stack size is 256 bytes
;--------------------

	.data
;--------------------------------------------
	validchars label byte					; name of the table
		db 32 dup(0)						; 
		db ' '								;
		db 13 dup(0)						;
		db '.'								;
		db 18 dup(0)						;
		db 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'		;
		db 6 dup(0)							;
		db 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'		;
		db 123 dup(0)						;
;--------------------------------------------

	.code				; code segment
;------------------------
; Establish addressability to the data segment.
; Initialize the work register.
;------------------------------------	
start:	mov ax,@data	     		; set up the DS registart.
		mov ds,ax		         	; moves ax to ds.
		mov bx,offset validchars	; move to offset of table.
;------------------------------------
; Reads the next character from the standard in.
; Checks if the character is a period to end the program.
; Checks if the character is a space and print it.
;------------------------
getl:	mov ah,8		; ah code for reading a character with no echo.
		int 21h			; calling the dos interupt.
		xlat validchars	; translates the chractes corrosponding with the table.
		or al,al		; or the al al.
		jz getl			; jumps if zero it getl
		mov dl,al		; moves al to dl.
		mov ah,2		; code to write character.
		int 21h			; calls dos interupt.
		cmp dl,2eh		; compares to see if period was entered.
		je exit			; jump to exit if equal.
		loop getl		; loop back to get another character.
;------------------------
; The user typed a period so terminate.
;------------------------
exit:	mov ax,4c00h	; setting ax to return dos.
		int 21h			; calling the dos interupt.
		end start		; end of the start.
;------------------------