This is a example problem in my book. Assuming pin 5 of port 4 is connected to an amplifier that drives a loudspeaker, the solution is given as,
The frequency of middle C is $$f=261.63\ \text{Hz}$$ So, the time period is, $$T=\frac1{f}=3822\ \mu s$$
The program to produce a square wave with that period is,
LOOP1: OUT 4H ;Send bit to speaker
MVI C,86H ;Set count to 134
LOOP2: DCR C ;Count down
JNZ LOOP2 ;Test count
CMA ;Reset bit 5
NOP ;Fine tune
NOP ;Fine tune
JMP LOOP1 ;Go for next half cycle
The number of T states is given as, OUT(10), MVI(7), DCR(4), JNZ(10 if true, else 7), CMA(4), NOP(4), JMP(10).
With a clock frequency of 1 MHz, LOOP2(for half cycles) runs for 1912 microseconds, which is close enough. LOOP1 should run again sending the complement of what was previously in bit 5 of port 4. But I think it doesn't.
When LOOP2 ends, the accumulator has 00H left over from C register. CMA changes the accumulator to FFH. NOP and JMP don't change the accumulator. So then the LOOP1 iterates for the next half cycle, OUT sents accumulator contents to port 4, i.e, FFH whose bit 5 is 1, everytime. So there is not a square wave, it's just a high signal. Then how does it produce a middle C?