Lab 4: Solutions

There is a lot of whitespace between the solutions. Use the menu to jump to the solution of interest.

Lab 4.1 Solutions

Inverse Triangle

This solution is simple because it uses the loop command.

Simple inverse triangle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
    ; Start instructions
    ; ---------------------------------------------
    ; Register information
    ; EAX:      ; temp var, the character to print
    ; EBX:      ; column counter (inner loop)
    ; ECX:      ; row counter (outer loop)
    ; EDX:      ; not used
    ; ---------------------------------------------
    mov     ecx, 5          ; ECX is i (this must be ECX for 'loop' instruction)
outer:
    mov     ebx,  0         ; reset j
  inner_start:
    cmp     ebx,  ecx       ; compare j and i
    jge     inner_end       ; if (j => i) exit inner loop
    inc     ebx             ; j++
    mov     eax,  ebx       ; print the counter (or mov a DX label here)
    call    print_int       ; Print j
    jmp     inner_start     ; continue inner loop
  inner_end:
    call    print_nl
    loop    outer           ; if EXC != 0, continue outer loop
    ; End instructions, clean up
Expected Output
12345
1234
123
12
1

Right Triangle

Right triangle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    ; Start instructions
    ; ---------------------------------------------
    ; Register information
    ; EAX:      ; column loop counter; the character to print
    ; EBX:      ; row loop control variable
    ; ECX:      ; row loop counter
    ; EDX:      ; column loop control variable
    ; ---------------------------------------------

    mov     ecx, 0          ; ECX is i, row counter
    mov     ebx, 5          ; ebx loop condition (number of rows)
row:
    cmp     ecx,  ebx       ; compare i and number of rows
    jg      row_end         ; if (i > rows) exit loop

    ; equilateral triangle
    mov    edx,  ecx        ; set column control var equal to the row

    mov     eax,  0         ; reset column counter
    call    column          ; print the column data
    call    print_nl
    inc     ecx             ; i++
    jmp     row             ; goto row start
row_end:
    jmp     end             ; all done

; print the column data
column:
    cmp     eax,  edx       ; compare j and i
    jge     return          ; if (j => i) call "ret"
    inc     eax             ; j++
    call    print_int       ; Print loop counter in EAX
    jmp     column          ; continue column loop

return:
    ret                     ; return to the last address called

end:
    ; End instructions, clean up
Expected Output
1
12
123
1234
12345

Scroll down for next solution:































Lab 4.2 Solutions

Wide Triangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    ; Start instructions
    ; ---------------------------------------------
    ; Register information
    ; EAX:      ; column loop counter; the character to print
    ; EBX:      ; row loop control variable
    ; ECX:      ; row loop counter
    ; EDX:      ; column loop control variable
    ; ---------------------------------------------

    mov     ecx,  0         ; ECX is i, row counter
    mov     ebx,  5         ; ebx loop condition (number of rows)
row:
    cmp     ecx,  ebx       ; compare i and number of rows
    jg      row_end         ; if (i > rows) exit loop

    ; wide triangle
    mov    eax,   3         ; multiplication factor
    mul    ecx              ; multiple the factor (EAX) by the loop counter in ECX
                            ; to print a wide triangle
    mov    edx,   eax       ; put the number of columns to print in EDX

    mov     eax,  0         ; reset column counter
    call    column          ; print the column data
    call    print_nl
    inc     ecx             ; i++
    jmp     row             ; goto row start
row_end:
    jmp     end             ; all done

; print the column data
column:
    cmp     eax,  edx       ; compare j and i
    jge     return          ; if (j => i) call "ret"
    inc     eax             ; j++
    push    eax             ; save counter
    mov     eax,   char     ; get character to print
    call    print_string    ; Print *
    pop     eax             ; restore counter
    jmp     column          ; continue column loop

return:
    ret                     ; return to the last address called

end:
    ; End instructions, clean up
Wide Triangle Expected Results (i * 3)
1
2
3
4
5
6
*
* * * *
* * * * * * *
* * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * * *

Scroll down for next solution:































Lab 4.3 Solutions

Square Curve

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    ; Start instructions
    ; ---------------------------------------------
    ; Register information
    ; EAX:      ; column loop counter; the character to print
    ; EBX:      ; row loop control variable
    ; ECX:      ; row loop counter
    ; EDX:      ; column loop control variable
    ; ---------------------------------------------

    mov     ecx,  0         ; ECX is i, row counter
    mov     ebx,  6         ; ebx loop condition (number of rows)
row:
    cmp     ecx,  ebx       ; compare i and number of rows
    jg      row_end         ; if (i > rows) exit loop

    ; Square curve
    mov     eax, ecx        ; Get the row
    mul     ecx             ; EAX = row*row
    mov     edx, eax        ; put the number of columns to print in EDX

    mov     eax,  0         ; reset column counter
    call    column          ; print the column data
    call    print_nl
    inc     ecx             ; i++
    jmp     row             ; goto row start
row_end:
    jmp     end             ; all done

; print the column data
column:
    cmp     eax,  edx       ; compare j and i
    jg      return          ; if (j => i) call "ret"
    inc     eax             ; j++
    push    eax             ; save counter
    mov     eax,   char     ; get character to print
    call    print_string    ; Print *
    pop     eax             ; restore counter
    jmp     column          ; continue column loop

return:
    ret                     ; return to the last address called

end:
    ; End instructions, clean up
Square curve (n * n) Expected Results
1
2
3
4
5
6
7
*
* *
* * * * *
* * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Scroll down for next solution:































Lab 4.4 Solutions

Skinny Triangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    ; Start instructions
    ; ---------------------------------------------
    ; Register information
    ; EAX:      ; column loop counter; the character to print
    ; EBX:      ; row loop control variable
    ; ECX:      ; row loop counter
    ; EDX:      ; column loop control variable
    ; ---------------------------------------------

    mov     ecx,  1         ; ECX is i, row counter
    mov     ebx,  21        ; ebx loop condition (number of rows)
row:
    cmp     ecx,  ebx       ; compare i and number of rows
    jg      row_end         ; if (i > rows) exit loop

    ;
    ; skinny triangle   ** ECX must start at 1, not 0. **
                            ; EAX = EAX/ECX; columns = rows/x
    mov     eax,  ecx       ; move the current row value into EAX
    push    ecx             ; save current row value
    mov     ecx,  5         ; put our divisor in ECX
    mov     edx,  0         ; initialize the EDX (where the remainder goes)
    div     ecx             ; EAX = EAX / ECX
    mov     edx,  eax       ; put the number of columns to print in EDX
    pop     ecx             ; restore the row value to ECX
    ;
    mov     eax,  0         ; reset column counter
    call    column          ; print the column data
    call    print_nl
    inc     ecx             ; i++
    jmp     row             ; start new row
row_end:
    jmp     end             ; all done

; print the column data
column:
    cmp     eax,  edx       ; compare j and i
    jg      return          ; if (j => i) call "ret"
    inc     eax             ; j++
    push    eax             ; save counter
    mov     eax,   char     ; get character to print
    call    print_string    ; Print *
    pop     eax             ; restore counter
    jmp     column          ; continue column loop

return:
    ret                     ; return to the last address called

end:
    ; End instructions, clean up
The Burj Khalifa! n/5
*
*
*
*
**
**
**
**
**
***
***
***
***
***
****
****
****
****
****
*****
*****