-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Part 2 chapter 7 - code only #93
base: master
Are you sure you want to change the base?
Conversation
I wonder if a 8/8 division algorithm would be more or less complicated to teach than BCD and ; Increase score by 1
IncreaseScore:
ld a, [wScore]
inc a
ld [wScore], a
ret
; Read the score from wScore and update the score display
UpdateScoreBoard:
; Divide score by 10
; The quotient is the tens digit, the remainder is the ones digit
ld a, [wScore]
ld h, a ; h = dividend
ld l, 10 ; l = divisor
; Divide h by l, putting the quotient in h and the remainder in l
; Dividing by 10 means that the quotient is the tens digit
; and the remainder is the ones digit
call DivideHByL
; Show the tens digits on screen
ld a, h ; quotient
add a, DIGIT_OFFSET
ld [SCORE_TENS], a
; Show the ones digit on screen
ld a, l ; remainder
add a, DIGIT_OFFSET
ld [SCORE_ONES], a
ret
; Divide h by l, returning the quotient in h and the remainder in l
DivideHByL:
ld a, 0 ; Initialize the remainder to 0
ld b, 8 ; Loop over each bit in the dividend
DivideLoop:
; Multiply h by 2, overflowing into a
sla h
rl a
; If the remainder so far is not less than the divisor...
cp a, l
jp c, ContinueDivideLoop
; ...then subtract the divisor from the remainder
sub a, l
; and increment the quotient
inc h
ContinueDivideLoop:
dec b
jp nz, DivideLoop
ld a, l ; Return the remainder in l
ret |
This would also be an easy entry into BCD manipulation, if that's what the tutorial is supposed to explain. For teaching purposes, especially if someone is new to assembly, the BCD approach might be easier to grasp. One could ask the reader to accept that A bit off-topic maybe, but my personal opinion would be that if you need a large score range and want the digit extraction to be clear, the division approach would be better, especially while increasing the score. If the range is small (1 byte,) BCD might be a bit faster for displaying. |
I'd say this is a great opportunity to iterate on the suggested approach, so we can start with BCD and later propose an improved version ! |
Hi there! I thought I'd have a go at #81.
Before adding any text and explanation to the docs, I thought I'd see first if this is what you were looking for. I did add some explanation in code comments and would be happy to update the teaching materials accordingly, once we agree on the code.
I've used packed BCD in just one byte. The actual code is in 40692b2 at labels
IncreaseScorePackedBCD
andUpdateScoreBoard
Looking forward to your questions and comments.