Thank you, Peter Cordes, for your invaluable feedback!
Your observation regarding the correct register usage for the WriteChar
procedure was exactly what I needed to resolve the issues I was facing with my Pascal's Triangle program.
Initially, my program correctly displayed Pascal's Triangle up to Row 5. However, starting from Row 6 onwards, the output became garbled with concatenated numbers and random symbols. This was primarily due to incorrect handling of the space character between binomial coefficients.
As you pointed out, the WriteChar
procedure from the Irvine32 library expects the character to be in the AL
register, not DL
. In my original code, I was moving the space character into DL
, which led to incorrect character printing.
Corrected Register for WriteChar
:
Original Code:
; Print Space
MOV DL, 32 ; ASCII space character (decimal 32)
CALL WriteChar ; Print space
Updated Code:
; Print Space
MOV AL, 32 ; ASCII space character (decimal 32)
CALL WriteChar ; Print space
Explanation:
By moving the space character (32
) into the AL
register instead of DL
, the WriteChar
procedure correctly prints the space, ensuring proper separation between binomial coefficients.
Verified Register Preservation:
EAX
, EDX
, ESI
, EDI
) are properly preserved at the beginning of procedures and restored before exiting. This prevents unintended side effects from procedure calls like WriteDec
and WriteChar
.After implementing the changes, the program now correctly displays Pascal's Triangle with proper spacing between numbers. Here's an example of the output when entering 13
rows:
Pascal's Triangulator - Programmed by Cameron Brooks!
This program will print up to 13 rows of Pascal's Triangle, per your specification!
Enter total number of rows to print [1...13]: 13
Row 0: 1
Row 1: 1 1
Row 2: 1 2 1
Row 3: 1 3 3 1
Row 4: 1 4 6 4 1
Row 5: 1 5 10 10 5 1
Row 6: 1 6 15 20 15 6 1
Row 7: 1 7 21 35 35 21 7 1
Row 8: 1 8 28 56 70 56 28 8 1
Row 9: 1 9 36 84 126 126 84 36 9 1
Row 10: 1 10 45 120 210 252 210 120 45 10 1
Row 11: 1 11 55 165 330 462 462 330 165 55 11 1
Row 12: 1 12 66 220 495 792 924 792 495 220 66 12 1
Thank you for using Pascal's Triangulator. Goodbye!
Understanding Library Procedures:
It's crucial to thoroughly understand how library procedures like WriteChar
expect their arguments. Misplacing data in the wrong registers can lead to unexpected behaviors.
Register Management:
Proper preservation and restoration of registers are essential to maintain data integrity across procedure calls in Assembly language.
Thanks to Peter's guidance, the program now functions as intended, accurately displaying Pascal's Triangle with proper spacing between numbers. If anyone has further suggestions or improvements, I'd be happy to hear them!