Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SPARK 3.59 KB | None | 0 0
  1.     .global displayDiamond
  2.     .section ".text"
  3.  
  4. /*
  5.  * Function name: displayDiamond()
  6.  * Function prototype: void displayDiamond( long width, long height,
  7.  *                      long borderCh, long displayCh);
  8.  * Description: Prints out a diamond with displayCh in a box of borderCh.
  9.  *      The box has a width and height passed in as parameters.
  10.  * Parameters:
  11.  *  arg 1: long width -- the width of the box
  12.  *  arg 2: long height -- the height of the box
  13.  *  arg 3: long borderCh -- the char to be used for the border
  14.  *  arg 4: long displayCh -- the char to use to for the diamond
  15.  *
  16.  * Side Effects: Prints out a diamond of displayCh in a box of borderCh
  17.  * Error Conditions: None
  18.  * Return Value: void
  19.  *
  20.  * Registers Used:
  21.  *  %i0 - arg 1, the width
  22.  *  %i1 - arg 2, the height
  23.  *  %i2 - arg 3, borderCh
  24.  *  %i3 - arg 4, displayCh
  25.  *  %o0 - Used to call printChar, whichever char to display is stored here
  26.  *        Also used to call .div for %l6
  27.  *  %o1 - Used to calculate %l6, store 2 in %o1
  28.  *  %l0 - column
  29.  *  %l1 - newline Char, /n
  30.  *  %l2 - height - 2
  31.  *  %l3 - row
  32.  *  %l4 - inner
  33.  *  %l5 - outer
  34.  *  %l6 - (width - row) / 2
  35.  */
  36.  
  37. displayDiamond:
  38.     save    %sp, -96, %sp   ! Save caller's window; if different than -96
  39.                 ! then comment on how that value was calculated
  40.  
  41.                 ! Initilizates variables
  42.  
  43.     mov '\n', %l1   ! Set newline char as '\n'
  44.     sub %i1, 2, %l2 ! Sets %l2 which is height - 2
  45.  
  46.                 ! Loop for printing the top line of the box
  47.     mov 1, %l0      ! col = 1
  48.     mov %i2, %o0    ! Set current char as border ch
  49.  
  50.     cmp %l0, %i0   
  51.     bg  end_top_loop    ! If col > width
  52.     nop
  53.  
  54. top_loop:           ! Loop that prints the top row of border
  55.     call    printChar   ! Should print borderCh
  56.     nop
  57.  
  58.     inc %l0     ! Increment col
  59.  
  60.     cmp %l0, %i0   
  61.     ble top_loop    ! If col <= width
  62.     nop
  63.  
  64. end_top_loop:
  65.  
  66.     mov %l1, %o0    ! Set current char as newline char
  67.  
  68.     call    printChar   ! Print newline char once
  69.     nop
  70.  
  71.                 ! Loop for printing the top half of the
  72.                 ! diamond & box
  73.  
  74.     mov 1, %l3      ! row = 1
  75.     cmp %l3, %l2    ! Comparing row & (height - 2) 
  76.     bg  top_half_loop_end   ! Call the end for the outermost loop
  77.     nop
  78.  
  79. top_half_loop:
  80.  
  81.     sub %i0, %l3, %o0   ! %o0 = width - row
  82.     mov 2, %o1      ! For calling .div
  83.  
  84.     call    .div        ! %o0 = (width - row) / 2
  85.     nop
  86.  
  87.     mov %o0, %l6    ! Now %l6 = (width - row) / 2
  88.  
  89.     mov %l6, %l5    ! outer = (width - row) / 2
  90.  
  91.     cmp %l5, 1
  92.     bl  top_half_inner1_end ! If outer < 1
  93.     nop
  94.  
  95. top_half_inner1:        ! Prints out borderChar
  96.    
  97.     mov %i2, %o0    ! Sets current char as border char
  98.  
  99.     call    printChar   ! Print out border char
  100.     nop
  101.  
  102.     dec %l5     ! Decrement outer
  103.  
  104.     cmp %l5, 1
  105.     bge top_half_inner1 ! If outer >= 1
  106.     nop
  107.  
  108. top_half_inner1_end:
  109.  
  110.     mov 1, %l4      ! inner = 1
  111.  
  112.     cmp %l4, %l3    ! Compare inner & row
  113.     bg  top_half_inner2_end ! If inner > row
  114.     nop
  115.  
  116. top_half_inner2:        ! Prints out diamond char
  117.  
  118.     mov %i3, %o0    ! Set current char as display char
  119.  
  120.     call    printChar   ! Print out display char
  121.     nop
  122.  
  123.     inc %l4     ! Increment inner
  124.  
  125.     cmp %l4, %l3
  126.     ble top_half_inner2 ! If inner >= row loop again
  127.     nop
  128.  
  129. top_half_inner2_end:
  130.  
  131.     mov %l6, %l5    ! outer = (width - row) / 2
  132.  
  133.     cmp %l5, 1
  134.     bl  top_half_inner3_end ! If outer < 1
  135.     nop
  136.  
  137. top_half_inner3:        ! Prints out border char
  138.    
  139.     mov %i2, %o0    ! Sets current char as border char
  140.  
  141.     call    printChar   ! Print out border char
  142.     nop
  143.    
  144.     dec %l5     ! Decrement outer
  145.  
  146.     cmp %l5, 1
  147.     bge top_half_inner3 ! If outer >= 1
  148.     nop
  149.  
  150. top_half_inner3_end:
  151.                 ! End of all the inner loops
  152.     mov %l1, %o0    ! Set current char as newline char
  153.  
  154.     call    printChar   ! print newline
  155.     nop
  156.  
  157.     inc %l3
  158.     inc %l3     ! Equivalent to row += 2
  159.  
  160.     cmp %l3, %l2
  161.     ble top_half_loop
  162.     nop
  163.  
  164. top_half_loop_end:
  165.  
  166.     ret
  167.     restore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement