-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|511|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Blogs Home -> Blogs


 
power mousey
Created : 12 November 2007
 

single loops....



This has to do with assembly language.
And with 6502 assembly language in particular.

Without much further ado
here are 2 loops in 6502 assembly language.
Explanations will follow....




and for the second snippet



now, a run down for each loop.

The first loop is known as you might guessed it
as a Count Up loop. First create an entry label for the loop. This is good for a short comment.
The entry label is not only just for an identifier label
but is useful to call from a assembly language program to a subroutine which conntains a loop like this or similar. If the loop is contained within a subroutine, the end of a subroutine contains or should contain a RTS instruction.
RTS means Return from Subroutine.

continuing onward......
the contents of register X is loaded with immediate data
of 0(deciamal or base 10. Then, the label 'Increment' is encountered and then the instruction of inx is performed.
The contents of register X is incremented by 1.
Then the instruction of cpx #10 is performed.
This instruction takes the contents in register X and is subtracted by the immediate data value in the compare instruction. If the result is 0 then the zero flag is set to 1. If the value is anything else the zero flag is set to 0...for a False condition.
1 for a TRUE condition, and 0 for a FALSE condition.

finally, the instruction 'bne Increment' is performed.
This is a branch instruction. It means branch to the label
assigned as 'Increment' if the zero flag is Not Equal to 1.
Since the contents of the register X contain the value of 1
the zero flag is set to 0...signifying a FALSE condition.
So, it branches back to the label 'Increment' and follows thru and performs another increment, checks the state or condition of the zero flag and branches back to the label 'Increment'.
Until a 10 is in the contents of the register X.
Now, the compare x instruction with 10 reuslts in a value
of 0 and the zero flag is set to TRUE or a 1 for a TRUE condition.
Now when the branch instruction is performed it cheks the state of the zero flag. Since the zero flag is set to 1 for a True condition the rest of the branch instruction is ignored. Since the value in the X register is equal to the value in the compare x instruction, and the subtraction results in a zero and setting the zero flag to 1 that there was an operation that resulted in a zero result.

Next, I will explain and go thru the second loop.

 

Comments


Monday, 12 November 2007, 10:41
power mousey
okay....

now for the Count down loop.

here is the snippet again.



again, the loop starts with an entry label
that introduces this loop if it was set up as procedure or subroutine and with the associative RTS(Retrun from Subroutine) at the end. Again, it also acts like a short comment or self identifying tag too.
next with the ldx #10 instruction. this loads the contents
of register X with the immendiate data of 10(base 10 or decimal).
after this, the 'Decrement' label is encountered and the program flows to the next instruction of dex.
This dex instruction decrements the contents of register X. And in this particular time moment of the loop...the contents of register X is now 9.
the branch instruction is performed next.
bne Decrement checks the state or condtion of the zero flag. Since the last operation: a subtraction of deCrementing the contents of regiser X results in 9, the zero flag is set/rest to False..which is a 0 state.
Since the branch instruction 'branches' on Not Equal to zero the program flow goes back to the label of 'Decrement'.
This action repeats until a zero is placed into the contents of the reister X and the branch instruction checks the state of the zero flag.
Since the contents of register X ocntain a 0, the zero flag is set to a 1 for a True condition.
SO the whole branch instruction falls thru and the branch to the label of 'Decrement' is ignored cause the branch instrucion only is perforned on a Not Equal to zero condition in the zero flag. Since the zero flag contains a 1 for a True condition...since the contents of register X contain a zero now.
Monday, 12 November 2007, 11:11
power mousey
you can use both loops to achieve
the same thing.
Yet, the second loop is a lot better
and the preferred method of looping in 6502
assembly language.

The reasons being are due to it takes 2 less bytes
and also 2 less processing cycles. The culprit is in the Count Up loop and with the cpx instruction. This is 2 bytes long and takes an extra 2 cycles to process.

although, assembly language is blazing fast.


here is a snippet of a double loop



by the way NOP is an instruction that does nothing.....
or rather that pauses the CPU for certain number of cycles.
NOP stand s for No Operation--> No OPeration.