Skip to content
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

Fix MultiplyNumbers macro #3

Closed
wants to merge 1 commit into from
Closed

Fix MultiplyNumbers macro #3

wants to merge 1 commit into from

Conversation

ISSOtm
Copy link
Contributor

@ISSOtm ISSOtm commented Jan 6, 2020

It used to shift the number left 11 times, not multiply it.

It used to shift the number left 11 times, not multiply it.
Copy link
Owner

@ahrnbom ahrnbom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this was a big oversight.

In the push bc line, modify the comment to mention that we can use both B and C, not just B.

In the macro, the line ld bc, (\2) << 8 | (\1) is not readable for beginners. I suggest either adding a comment explaining what happens (like ; equivalent to ld b, \2 followed by ld c, \1 or just do

ld b, \2
ld c, \1 ; a more efficient implementation would be "ld bc, (\2) << 8 | (\1)"

or something along those lines.

@ahrnbom
Copy link
Owner

ahrnbom commented Jan 7, 2020

As of 78910fb, I ended up fixing it myself. It's a slightly different solution than yours (when it comes to the macro) which I think is easier for beginners to understand. Thanks very much for pointing out this (and the other!) flaws.

@JL2210
Copy link

JL2210 commented May 30, 2020

@ahrnbom You didn't actually fix it, you just documented it as fixed. It's still present in the latest Git revision.

@ahrnbom
Copy link
Owner

ahrnbom commented May 31, 2020

No, the error has been fixed, just not in the way suggested by the pull request.

The current code in the book is

MultiplyNumbers: MACRO
    push bc

    ld a, \1 ; Store X in A 
    ld b, (\2-1) ; Store Y-1 in B
    ld c, a 
    
.loop\@:
    add c ; Add X
    dec b
    jr nz, .loop\@
    
    pop bc     
ENDM

which I have confirmed to work.

@JL2210
Copy link

JL2210 commented May 31, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants