Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .global displayDiamond
- .section ".text"
- /*
- * Function name: displayDiamond()
- * Function prototype: void displayDiamond( long width, long height,
- * long borderCh, long displayCh);
- * Description: Prints out a diamond with displayCh in a box of borderCh.
- * The box has a width and height passed in as parameters.
- * Parameters:
- * arg 1: long width -- the width of the box
- * arg 2: long height -- the height of the box
- * arg 3: long borderCh -- the char to be used for the border
- * arg 4: long displayCh -- the char to use to for the diamond
- *
- * Side Effects: Prints out a diamond of displayCh in a box of borderCh
- * Error Conditions: None
- * Return Value: void
- *
- * Registers Used:
- * %i0 - arg 1, the width
- * %i1 - arg 2, the height
- * %i2 - arg 3, borderCh
- * %i3 - arg 4, displayCh
- * %o0 - Used to call printChar, whichever char to display is stored here
- * Also used to call .div for %l6
- * %o1 - Used to calculate %l6, store 2 in %o1
- * %l0 - column
- * %l1 - newline Char, /n
- * %l2 - height - 2
- * %l3 - row
- * %l4 - inner
- * %l5 - outer
- * %l6 - (width - row) / 2
- */
- displayDiamond:
- save %sp, -96, %sp ! Save caller's window; if different than -96
- ! then comment on how that value was calculated
- ! Initilizates variables
- mov '\n', %l1 ! Set newline char as '\n'
- sub %i1, 2, %l2 ! Sets %l2 which is height - 2
- ! Loop for printing the top line of the box
- mov 1, %l0 ! col = 1
- mov %i2, %o0 ! Set current char as border ch
- cmp %l0, %i0
- bg end_top_loop ! If col > width
- nop
- top_loop: ! Loop that prints the top row of border
- call printChar ! Should print borderCh
- nop
- inc %l0 ! Increment col
- cmp %l0, %i0
- ble top_loop ! If col <= width
- nop
- end_top_loop:
- mov %l1, %o0 ! Set current char as newline char
- call printChar ! Print newline char once
- nop
- ! Loop for printing the top half of the
- ! diamond & box
- mov 1, %l3 ! row = 1
- cmp %l3, %l2 ! Comparing row & (height - 2)
- bg top_half_loop_end ! Call the end for the outermost loop
- nop
- top_half_loop:
- sub %i0, %l3, %o0 ! %o0 = width - row
- mov 2, %o1 ! For calling .div
- call .div ! %o0 = (width - row) / 2
- nop
- mov %o0, %l6 ! Now %l6 = (width - row) / 2
- mov %l6, %l5 ! outer = (width - row) / 2
- cmp %l5, 1
- bl top_half_inner1_end ! If outer < 1
- nop
- top_half_inner1: ! Prints out borderChar
- mov %i2, %o0 ! Sets current char as border char
- call printChar ! Print out border char
- nop
- dec %l5 ! Decrement outer
- cmp %l5, 1
- bge top_half_inner1 ! If outer >= 1
- nop
- top_half_inner1_end:
- mov 1, %l4 ! inner = 1
- cmp %l4, %l3 ! Compare inner & row
- bg top_half_inner2_end ! If inner > row
- nop
- top_half_inner2: ! Prints out diamond char
- mov %i3, %o0 ! Set current char as display char
- call printChar ! Print out display char
- nop
- inc %l4 ! Increment inner
- cmp %l4, %l3
- ble top_half_inner2 ! If inner >= row loop again
- nop
- top_half_inner2_end:
- mov %l6, %l5 ! outer = (width - row) / 2
- cmp %l5, 1
- bl top_half_inner3_end ! If outer < 1
- nop
- top_half_inner3: ! Prints out border char
- mov %i2, %o0 ! Sets current char as border char
- call printChar ! Print out border char
- nop
- dec %l5 ! Decrement outer
- cmp %l5, 1
- bge top_half_inner3 ! If outer >= 1
- nop
- top_half_inner3_end:
- ! End of all the inner loops
- mov %l1, %o0 ! Set current char as newline char
- call printChar ! print newline
- nop
- inc %l3
- inc %l3 ! Equivalent to row += 2
- cmp %l3, %l2
- ble top_half_loop
- nop
- top_half_loop_end:
- ret
- restore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement