#-------------------------------------------------------------------------------
# Thomas C. Bressoud
# 29 January 2006
# ifthenelse.s
#
# Description:  Program to demonstrate an assembly translation of an while
#               construct.
#-------------------------------------------------------------------------------

# Next directive defines additions to the data segment

.data
.align 2		; Start on 4 byte boundary
loopcnt:
	.long	12	; Loop count variable
loopmsg:
	.asciz "Iteration: "
endmsg:
	.asciz "Loop has terminated\n"

# Next directive defines additions to the text (code) segment

.text
.align 2		; Start on a 4 byte boundary
.globl	_main	; Make the _main symbol visible outside this module
_main:
	.line 27
	mflr	r0	; Put the return address from main into r0
	stw		r0,8(r1)	; Save in the linkage of the callers stack frame
	stwu	r1,-64(r1)	; Allocate a new stack frame, updating the stack ptr
	stw		r14,32(r1)
	stw		r15,40(r1)

; {loopcnt >= 0}
; iteration = 0;
; while (loopcnt > 0) 
;   print_string(loopmsg);
;   println_int(iteration);
;   iteration++;
;   loopcnt--;
; }
; print_string(endmsg);

	; r4 = loopcnt
	lis		r4, ha16(loopcnt)
	lwz		r14, lo16(loopcnt)(r4)
	; r5::iteration = 0
	li		r15, 0
	
.globl looptop
looptop:	
	cmpwi	cr0,r14,0  			;if (r5::A <= r6::B) goto notif
	ble		loopend
	
	lis		r3,hi16(loopmsg)	;   r3 = loopmsg
	ori		r3,r3,lo16(loopmsg)
	
	bl		_print_string		;   print_string(r3::loopmsg)
	mr		r3, r15				;   r3 = r5::iteration
	bl		_println_int		;   println_int(r3::iteration)
	addi	r15,r15,1 			;   r5::iteration = r5::iteration + 1
	addi	r14,r14,-1			;   r4::loopcnt = r4::loopcnt - 1
	
	b		looptop				;   goto looptop

.globl loopend
loopend:						; {loopcnt <= 0}
	lis		r3,hi16(endmsg)		; 
	ori		r3,r3,lo16(endmsg)	; r3 = endmsg
	
	bl		_print_string		; print_string(r3::endmsg)

	lwz		r14,32(r1)
	lwz		r15,40(r1)
	addi	r1, r1, 64			; Epilog
	lwz		r0, 8(r1)			;
	mtlr	r0					;
	blr							; return

