I am a freshman studying computer science. In computer engineering we are working on a Zilog Z80 8-bit microprocessor (1MHz) and a set of components that need to be manually connected using a breadboard and cables.
The connecting part doesn't worry me, but I do have questions concerning the Assembly program that I need to write to get my programm working (LED running light, with manual input for behaviour and frequency).
I have already read the manual and know the set of instructions that can be used (only the bare necessities). For a start, I am not trying to get the cleanest, best looking code possible; but not to worry, I'll prettify it later on, since I like clean and efficient code.
For the moment, the program seems to run well in the simulator, so the syntax seems to be alright. Still, I am not sure how to progress with certain logical problems.
The exercise has the following specifications:
I have set the start address using ORG E000h
and initialized the stackpointer using MOV SP,FFFFh
. For the input (three different types of blinking/running, as well as two different frequencies, equaling five buttons in total) I have created different labels.
My problem at the moment is that I am not quite sure how to get my physical input right - IIRC, I'd need to specify a bit pattern by using XOR with everything being 1 but the desired input, so that I can use the information in my program.
But even though I do know the way it is supposed to work (at least I think that I know), I cannot quite wrap my head around the software implementation. Also, I have problems with conditions: Pressing one switch changes blinking frequency to 1/4 Hz, while pressing the other changes it to 4Hz. In higher-level languages I'd just use IF/ELSE here, but I don't know how to do it in this case - sadly, the manual only includes basic operations so I am at a loss.
Therefore, I thought I'd try my luck and ask the community for help.
For those interested, I'll post my code. It is very basic, as I already mentioned, but I just need it to get the job done for the time being. Since I am not a fan of huge chunks of clumsily formatted code, I have posted the file here. The file is a *.txt hosted through GoogleDrive.
Thank you for your time and have a nice day!
[EDIT] added specific code in post, according to input by user Ruud Helderman
[EDIT] updated code in *.txt-file - now simpler and more efficient
[EDIT] used HTML-formatting to highlight directives in post
Specific code snippet:
blink: ;function: all LED blinking, activated via input[2]
MOV A,FFh
OUT 05h,A ;all LED out
CALL pause1 ;frequency 1/4Hz, activated via input[5]
MOV A,00h
OUT 05h,A ;all LED on
CALL pause1
JP blink ;jump back to begin of function
The above function changes LED behavior (in this case: blinking) and also frequency using different specific physical switches on an input switch board with eight switches total (1 to 8, inactivate state = 1; switches 2 to 6 are used). I know that getting input should be a piece of cake - it should just be a matter of using XOR with bit patterns of 0 and exactly one 1.
While trying to find a solution for my problem I came upon different approaches online, such as using TEST
to check for bits on specific locations. Still, my instruction manual has no mention of any such directive and the assignment itself doesn't mention it either.
I am quite aware that this is probably a trivial question, and maybe I am just stuck in a mental loop that I created myself by overthinking, but at the moment I don't know how to get to were I need to be (even though I can see the castle on the horizon - thanks Kafka!).
Any help is greatly appreciated.
After some days of pondering and racking my brain, and with the help of the great input provided by all of you, I finally found a solution to my problem. The biggest concern for me was, at the end, that I didn't know how to correctly check for input.
The problem was, as I had assumed, owed to my being stuck on a misconception, which fortunately was corrected by my lab partner. So, after all, we were able to get our program to work - last minute and not at 100%, but it worked and met the requirements.
So what was my misconception? Funny enough, I knew where we had to go, and the idea was correct. The problem was that I had skipped a major part of the logic operation - I had already calculated it in my head, and then combined the actual working solution with an obsolete AND
which destroyed functionality.
All in all, the correct combination of XOR
and AND
was as following:
programloop:
MOV A,40h ;state of button 2, inverted (XOR FFh)
MOV B,A ;save state to register B
IN A,03h ;input at port-address 03h
AND B ;find out if button is pressed
JPNZ blink ;if yes, jump to blink
MOV A,20h ;state of button 3, inverted (XOR FFh)
MOV B,A ;save state to register B
IN A,03h ;input at port-address 03h
AND B ;find out if button is pressed
JPNZ goright ;if yes, jump to goright
MOV A,10h ;state of button 4, inverted (XOR FFh)
MOV B,A ;save state to register B
IN A,03h ;input at port-address 03h
AND B ;find out if button is pressed
JPNZ goleft ;if yes, jump to goleft
JP programloop ;go back to beginning (input has to be checked constantly)
This did the trick concerning the three buttons that changed LED-behavior.
Concerning frequency, we had to reduce complexity down to only two states, due to heavy time constraints (we misread the assignment and erroneously started with the bonus question, which cost us about 50% of our development time - yay us. Lesson learned: always start reading at the top and read carefully.)
But since the changing of frequency worked, it turned out OK.
MOV A,03h ;state of button five being pressed (inverted)
MOV B,A ;saved state into register B for later use
IN A,03h ;physical input over button
AND B ;find out if button is pressed
JPNZ freq025Hz ;if yes, jump to freq025Hz
JPZ freq4Hz ;if no, jump to freq4Hz
So this is it!
Once again, thank you all for your help.
If any questions remain, feel free to ask!