-3

Given the following C code:

int a=8;
if (a==8)
    ...

will the equivalent in z80 assembly be the code below?

ld a, 8
cp 8
jww
  • 97,681
  • 90
  • 411
  • 885
gameloverr
  • 53
  • 1
  • 8
  • 3
    You probably need a `JR Z, NEXT-$` to jump to the next label if equal. – jww Dec 08 '19 at 18:12
  • so Z is if equal and NZ if its not equal? – gameloverr Dec 08 '19 at 18:38
  • The `Z` flag (Zero) is set when a calculation results in a zero value. In this case, it is when the comparison is equal, which can be thought of as the calculation of (`a` - 8) resulting in zero. – 1201ProgramAlarm Dec 08 '19 at 21:37
  • 3
    In principle you're right. But your compiler will have 16-bit `int` most probably. Then the shown assembly is not the same since it uses an 8-bit type. Just my $0.02, if you ever happen to look at compiler generated assembly. – the busybee Dec 09 '19 at 07:14
  • 2
    I would say, it is very unlikely that the "equivalent in z80 assembly" would look like those two instructions in your question. Generally speaking - it is pointless to discuss a couple of lines of C code which do not do anything useful (taken on their own these lines are logically equivalent to a NOP). Also, it is pretty much pointless to discuss C-to-asm translation without a particular compiler in mind. I can confirm that the compiler I worked with (SDCC) would use 16 bits to represent an 'int'. – tum_ Dec 09 '19 at 11:26
  • Your example code C does nothing, so technically, the equivalent assembly would be NOP or just nothing at all. – Stefan Paul Noack Dec 11 '19 at 14:02

1 Answers1

2

Just throwing some registers around:

   ld hl,8     ; int is a 16 bit value
   push hl
   push bc
   ld bc,8
   or a        : clear carry flag
   sbc hl,bc   ; zero flag set when equal
   pop bc
   pop hl
   jr nz,endif

   ...

endif:

   ...

For an interesting take on using macros for structured assembly programming see https://dev.to/jhlagado/structured-programming-in-z80-assembly-554d

Stefan Drissen
  • 3,266
  • 1
  • 13
  • 21
  • 1
    `or a` is just to clear the carry flag ahead of `sbc`? Is that because Z80 doesn't have a 16-bit sub *without* carry-in? – Peter Cordes Dec 30 '19 at 17:33
  • 1
    @Peter, that’s right—Z-80 has both `sub` and `sbc` in 8-bit, but no 16-bit `sub`. – prl Dec 30 '19 at 19:38