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

# Next directive defines additions to the data segment

.data
.align 2		; Start on 4 byte boundary
A:	.long	17	; Allocate a 4 byte data word
B:	.long	17  ; Allocate another 4 byte data word
ifmsg:
	.asciz "Message when the if-block evaluates to true\n"
notifmsg:
	.asciz "Message when the if-condition evaluates to false\n"
endmsg:
	.asciz "At code following the if-block\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:
	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,-32(r1)	; Allocate a new stack frame, updating the stack ptr

; if (A > B) {
;   print_string(ifmsg);
; } else {
;   print_string(notifmsg);
; }  
; print_string(endmsg);

	; r5 = A
	lis		r4, ha16(A)
	lwz		r5, lo16(A)(r4)
	; r6 = B
	lis		r4, ha16(B)
	lwz		r6, lo16(B)(r4)
	
	cmpw	cr0,r5,r6  			;if (r5::A <= r6::B) goto notif
	ble		notif
	
	lis		r3,hi16(ifmsg)		;   r3 = ifmsg
	ori		r3,r3,lo16(ifmsg)
	
	bl		_print_string		;   print_string(r3::ifmsg)
	b		ifend				;   goto ifend
	
notif:							; {A <= B}
	lis		r3,hi16(notifmsg)	;   r3 = notifmsg
	ori		r3,r3,lo16(notifmsg)
	
	bl		_print_string		;   print_string(r3::notifmsg)

ifend:							; fi

	lis		r3,hi16(endmsg)		; r3 = endmsg
	ori		r3,r3,lo16(endmsg)
	
	bl		_print_string		; print_string(r3::endmsg)

	addi	r1, r1, 32
	lwz		r0, 8(r1)
	mtlr	r0
	blr

