I am going to try and make a binary counting program, what would be some useful opcodes, or functions? I don't want anyone to post exact code, but it would be helpful to see the functions and maybe familiarize myself with assembly. My thoughts are that if I can make this I could make an encryption program later on, that takes any length string and encrypts it. To start off, if I were to use a spot in the memory and I wanted to post each bit (starting from left to right) and make it get rid of all the 0's before the first 1 it finds, so it would output "1 | 10 | 11" etc. what would be a good spot in the memory, and what would be the code to check a bits value?
So to check the value of the 7th (2^7 = 128s place) bit, you would mask with $80, then compare to zero, then rotate. Like so (called by "call ShowByte")


Code:
ShowByte:
    ;input byte is in c
    ld b,8
searchloop:
    ld a,$80
    and c
    jr nz,disploop
    rl c
    djnz searchloop
    ret
disploop:
    //display 0 if a=0, 1 otherwise
    rl c
    djnz disploop
    ret


I've omitted the display code as an exercise to you; that's not as important as understanding the code above. Does it make sense to you?
OK, I am going to see what parts I understand...here goes:

Code:
    ld b,8

loads register B with the decimal number 8


Code:
searchloop:

starts a loop with the name "searchloop" for use with a jump or call command


Code:
    ld a,$80

loads register A with the hexadecimal number 80


Code:
    and c

does an "and" bitwise boolean logic on register A with register C


Code:
    jr nz,disploop

jumps relative, "nz" no clue, to the header called "disploop"


Code:
    rl c

not sure


Code:
    djnz searchloop

goes back to the header "searchloop" and checks if register B is zero yet, if not it goes through another iteration


Code:
    ret

returns to the TIOS


Code:
disploop:

makes the header called "disploop"


Code:
    rl c

again not sure


Code:
    djnz disploop

goes back to the beggining of the header, or finishes with that "jump relative" command and returns to the original loop


Code:
    ret

another return statement, not sure why...return to the other loop which called it maybe?


anyways, tell me if I messed up any of those parts in there explanations, not really that advanced with this yet. I do have a question however (which may be answere by the "rl c" part of the code) why does it do an and with $80?
$80 is %10000000 in binary so when you and a byte with it, everything but bit 7 becomes 0. If bit 7 was 0 then the z flag will be set which is used by the jump statement after it. I'm pretty sure


Code:

    bit  7,c


would work better than


Code:

    ld  a,$80
    and  c


But Kerm knows more about it than me

Edit - nz is a conditional, it means "do this command if the z flag is not 0"

rl moves each bit to the left one place (ex %01001000 would become %10010000). It also puts the contents of the carry flag in bit 0 but that's more of a side effect in this routine
When you're rotating or shifting more than the bit place that you're testing, you need to be careful whether to use the sll/srl (shift left/right logical) or rl/rr (rotate left/right) opcodes, as some insert ones or zeros and some write or read the carry bit. I dunno why I didn't just bit 7,c; something's nagging at the back of my mind about speed, but for your purposes, bit 7,c is perfectly sufficient. Jr and jp can take all kinds of conditional flags, like z (jump only if zero flag is set), nz (jump only if zero flag is reset), c (jump only if carry bit is set), nc (jump only if carry bit is reset). There's also a bunch of other conditions like the half-carry bit that few people use, and also the p (positive) and n (negative) conditions that you can only use with jp, not with jr. Are you familiar with the difference between jr and jp?
I think I understand the code now, but would it destroy whatever number you started with or not? Jr-jump relative Jp-jump to a specific spot in the memory? Where Jr does something has to do with jumping to a spot relative to the beggining of the program, and Jp just says "goto 1337 in the memory". Am I right?

Edit:I looked online and found out Jp is short for jump Razz
_player1537 wrote:
I think I understand the code now, but would it destroy whatever number you started with or not? Jr-jump relative Jp-jump to a specific spot in the memory? Where Jr does something has to do with jumping to a spot relative to the beggining of the program, and Jp just says "goto 1337 in the memory". Am I right?

Edit:I looked online and found out Jp is short for jump Razz
Jr jumps relative to that instruction, not relative to the beginning of the program. Otherwise, you've got it right.
The benefit of jr is it uses two bytes instead of three, but the caveat is it can only go +/- 127 bytes relative to the current one.
would I be able to do something like

Code:

 ld b,8
Start:
 bit b,c
 //print out that bit
jp Start

or at least something close to that?
That would display all the bits (sorta); you don't have any bitshifting or rotation. Also note that you described djnz wrong - it is Decrement and Jump if Not Zero: it first decrements b, and then jumps only if b != 0.
You can't do "bit b,c". The bit value has to be a constant value 0-7.
ok thank you, then my code would be

Code:
 ld b,8
Start:
 bit b,c
 //print out that bit
djnz Start


also, why would I need to do bitshifting and rotation? Doesn't this do the exact same thing
calc84maniac wrote:
You can't do "bit b,c". The bit value has to be a constant value 0-7.
Quoted for emphasis. You have to rotate instead of moving the bit that you, well, bit. An alternative would be to rotate the mask to the right instead of the number itself to the left.
sorry I missed calc84's post
would I be able to do this for the text placing part?

Code:

 If a==1 ld hl,text1
 if a==0 ld hl,text0
 b_call(_Puts)
 rl c
 djnz disploop
 ret
text1:
 .db "1",0
text0
 .db "0",0
That's a good start, but two things. One, you're better off with the _PutC bcall for a single character, and two, you need to push and pop b and c around bcalls that will destroy your register.


Code:
    ld d,'0'
    or a
    jr z,isZero
    ld d,'1'
isZero:
    ld a,d
    push bc
        bcall(_PutC)
        pop bc
    rl c
    djnz disploop
    ret
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement