RevSoft Code Game

Moderator: MaxCoderz Staff

Post Reply

How many did you get right?

0~1
4
80%
2~4
1
20%
5~7
0
No votes
8~9
0
No votes
10
0
No votes
 
Total votes: 5

User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

RevSoft Code Game

Post by Jim e »

We had a code game over at RevSoft, but we ran out of ideas after a while and it became a routine contest. So since some of you don't drop by RevSoft that often, I'll post the question here so you can play. Most of it is finding the most optimised way in z80 code, others are knowledge of the cpu.

Don't cheat, I'll know if your cheating because I know who knows the answers and who doesn't. And be careful, some things that seem obvious or well known are not correct.

Last the questions are in no particular order, difficulty will vary as you progress.


Question 01 - Spencer's:
Turn an 8-bit signed number into a 16-bit signed number. You do this by propagating the sign bit all the way to the most significant bit.
%10110110 turns to %1111111110110110,
%00000001 turns to %0000000000000001

Code: Select all

inputs:  a  - 8-bit signed number
outputs: bc - same 16-bit signed number

Question 02 - Jim e's:

Code: Select all

  ld hl,(source)
  ld de,(destination)
  ld bc,(length)
  ldir
How many bytes is this, ANSWER WITH OUT LOOKING AT GUIDES!!! Also tell why your answer is correct!



Question 03 - NanoWar's:

Code: Select all

label: jr label
Will the hex value be A, B, C, D or E?
A. 18 FE
B. 18 FF
C. 18 00
D. 18 01
E. 18 02



Question 04 - Jim e's:
okay here is an easy one I expect everyone to get it.

Code: Select all

	di
	ld a,r
	ld c,a
	ld b,a
	ld hl,($8000)
	ld ix,($8002)
label:
	ex de,hl
	add ix,de
	djnz label
	ld a,r
Relative to C what does A contain at the end of this code.

Hint: its an equation I don't expect an exact number.



Question 05 - Jim e's:
Name the registers in the CPU. If they can be paired, name them by their pairs. DO IT FROM MEMORY!



Question 06 - Jim e's:
Super duper easy!

add hl,de

P/V flags detects Parity or overflow, Do it from memory.



Question 07 - KevinJB's:
Here's an easy one:

Code: Select all

 xor a
 ld (sram), a
 ld (sram+1), a
 ld (sram+2), a
 ld (sram+3), a
 ld hl, 255
 push hl
 push hl
 pop hl
 ld (sram), hl
 pop hl
 ld (sram + 1), hl
What in order does sram, sram + 1, sram + 2, and sram + 3 hold?



Question 08 - Jim e's: ( altered from the original )
Reverse the bits of C and place it into A. So %10100000 would be come %00000101
Do this without the RLA or RRA instruction.

FASTEST CODE WINS.



Question 09 - Stickmanofdoom's:
Write a routine that will add A to DE (unsigned) without an "add reg16,reg16" instruction like "add hl,de".
I've posted something about this before, but I can't come up with anything else that would fit this game.

Editor Jim e's Note: Speed determines winner, since probability may play a role in this, post multiple answers.



Question 10 - KevinJB's:
Explain each bit in the Flag register. Sound good?






That's the end of the quiz. I'll post the answers later, most of it is simple. Some of it is pretty tough. Feel free to post your own quiz questions.

To be nice Hide you answers when you post them, The board color is #cfcfbf, So to hide text it would be.
Hidden Text
Hidden Text 8)

I really want to see MC's brightest participate in this. Because even Spencer only got one correct. Haha, burned him while he is on vacation.








(Gratuitous blank space to reduce answer spoilage.)
Image
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

If you already know who knows what, why did you bother making this thread? :D
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

Answer to question 8: ld b,aligned_reverse_lut >> 8 \ ld a,(bc) :P
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

CoBB wrote:Answer to question 8: ld b,aligned_reverse_lut >> 8 \ ld a,(bc) :P
...You're an evil bastard. I shouda put a size limit.

Come on, Answer the rest or post a new question.
Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

These questions seem to be a bit unchallenging for me, although I'm really curious who can account for the undocumented flag bits by heart. :P

Okay, here's one I hope some will find interesting...

Question 11:
It is a well-known fact that

Code: Select all

call Routine
ret
can be optimised into

Code: Select all

jp Routine
However, you can't do this in every case. Present a practical (i. e. non-fabricated) scenario where this trick would lead to an error and explain why.
threefingeredguy
Calc King
Posts: 2195
Joined: Sun 27 Mar, 2005 4:06 am
Location: sleeping
Contact:

Post by threefingeredguy »

I knew the flags by heart.

To answer the question:
In a routine that pops the stack in order to get the data at the previous location. Something like a string routine like this
call drawstring
.db "Blah",0
ret
[...]
drawstring:
pop hl
bcall(_puts)
jp(hl)


Long explaination and example code.
Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

threefingeredguy wrote:I knew the flags by heart.
I looked at the Revsoft topic, and saw no correct solutions there. (Not even from Jim e! :shock:) You only recited the names but didn't explain as the question requested. No-one seemed to know how the N and H flags work, and also no-one explained what overflow means exactly. The general knowledge of the S flag is also quite shaky from the looks of it. And well, no-one knew what the 'undocumented' flags do, even though their value is quite predictable in most cases (they both do have a primary function).

Educate yourselves. ;)

As for your answer to 11, it's unfortunately irrelevant. Note that the original call has to be immediately followed by a ret! Simply replacing a lone call with a jump would be nonsense in any program.
threefingeredguy
Calc King
Posts: 2195
Joined: Sun 27 Mar, 2005 4:06 am
Location: sleeping
Contact:

Post by threefingeredguy »

:D I noticed that I didn't have the ret immediately after, I just couldn't think of anything else.
Image
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

If you would normally pass parameters to the subroutine on the stack, but instead of call sub \ ret you do jp sub, the subroutine will still expect the return address to be at the top of the stack, so its parameters will be misread and cause general troubles when it takes too much stuff off the stack.

Of course, in this case the programmer would be stupid for passing by stack and not calling... One would have to at least push the return address that the ret you saved would have gone to on the stack...

I guess this is kind of a "duh" situation, but it isn't fabricated!
ImageImage
Image
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

CoBB wrote:I looked at the Revsoft topic, and saw no correct solutions there. (Not even from Jim e! :shock:) You only recited the names but didn't explain as the question requested. No-one seemed to know how the N and H flags work, and also no-one explained what overflow means exactly.
We don't all write books on z80. I believe the title of your thesis was "Some Smart Shit" wasn't it?

For 11 I can only imagine a good reason being when the function being called expects there to be a return to the calling function. For example.

Code: Select all

Main
    call  check
;do stuff
    ret

Check:
    ld a,(somevaule)
    cp 3
    call z,somecrap
    cp 9
    call z,somecrap
    cp 17
    call z,somecrap
    cp 29
    ret nz
    jp somecrap

somecrap:
    ld hl,(anothervaule)
    inc hl
    ld (anothervaule),hl
    ld a,h
    or l
    ld a,0
    ret nz        ;return to check
    pop hl
    ret           ;return to main.
okay maybe not the best example or even the best answer, But Im not so sure that argument passing would falter. My reason is that this something that you don't directly use the SP register for rather an indexing register with sp in it. You could pass the arguments after the reutrn adress. example:

Code: Select all

function:
    push ix
    ld ix,sizeof_args
    add ix,sp
    ld sp,ix

;do stuff

    ld de,-sizeof_args
    add ix,de
    ld sp,ix
    pop ix
    ret
Even if the return address was on top of the stack it would still have to re-adjust the stack level.

Not really sure on the situation where that would fail. It'd be rather particualr I'd imagine.
Image
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

Jim e wrote:We don't all write books on z80. I believe the title of your thesis was "Some Smart Shit" wasn't it?
:lol:

Note that I use the word 'smart' in the same sense as Ben. :P Seriously, everyone who writes some Z80 code should be perfectly familiar with the S, Z, P/V and C flags at least. The other four are much less important, being either 'undocumented' or only used by DAA.

These examples are certainly better. I encountered this very problem when coding some collision detection that needed to run identical code with different parameters four times. This code also called a subroutine, and it was possible to determine the outcome of the whole thing within this second call, allowing a fast pop \ ret escape. It took some time to hunt down that bug. :oops:
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

Its pretty much what I saw over done in the loom source. I wish wakamol the best of luck. :D

Well original this was more of a competition rather than a quiz, so best answer is the correct answer. :roll: 1, 2, 4, and 9 are the ones I like, But for some god awfull reason I couldn't write 8 for the answer I wanted....
Image
Post Reply