git @ Cat's Eye Technologies SixtyPical / 94ee042
Fix order of operands in word-sized `cmp`. Chris Pressey 6 years ago
4 changed file(s) with 14 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
397397
398398 Subtracts the contents of src from dest (without considering carry) but
399399 does not store the result anywhere, only sets the resulting flags.
400 This means that `z` is set if src and dest are equal,
401 and `c` is set if dest is greater than or equal to src
402 (`c` is unset if dest is less than src.)
400403
401404 * It is illegal if src OR dest is uninitialized.
402405
406409 In addition, if dest is of `word` type, then src must also be of `word`
407410 type, and in this case this instruction trashes the `a` register.
408411
409 Note that, like `cmp` is not suitable for making a
412 Note that `cmp` is not suitable for making a
410413 signed comparison; this article, which mentions
411414 techniques that a SixtyPical compiler could use to
412415 implement `cmp`, also explains why that is:
5151
5252 ld a, 20
5353
54 cmp a, b // 20 >= 21
54 cmp a, b // 20 >= 20
5555 if c {
5656 ld a, 71 // G
5757 call chrout
6262
6363 ld a, 19
6464
65 cmp a, b // 19 < 21
65 cmp a, b // 19 < 20
6666 if c {
6767 ld a, 71 // G
6868 call chrout
393393 if isinstance(src, LocationRef) and src.type == TYPE_WORD:
394394 src_label = self.get_label(src.name)
395395 dest_label = self.get_label(dest.name)
396 self.emitter.emit(LDA(Absolute(src_label)))
397 self.emitter.emit(CMP(Absolute(dest_label)))
396 self.emitter.emit(LDA(Absolute(dest_label)))
397 self.emitter.emit(CMP(Absolute(src_label)))
398398 end_label = Label('end_label')
399399 self.emitter.emit(BNE(Relative(end_label)))
400 self.emitter.emit(LDA(Absolute(Offset(src_label, 1))))
401 self.emitter.emit(CMP(Absolute(Offset(dest_label, 1))))
400 self.emitter.emit(LDA(Absolute(Offset(dest_label, 1))))
401 self.emitter.emit(CMP(Absolute(Offset(src_label, 1))))
402402 self.emitter.resolve_label(end_label)
403403 return
404404 cls = {
395395 | {
396396 | cmp za, zb
397397 | }
398 = $080D LDA $081C
399 = $0810 CMP $EA61
398 = $080D LDA $EA61
399 = $0810 CMP $081C
400400 = $0813 BNE $081B
401 = $0815 LDA $081D
402 = $0818 CMP $EA62
401 = $0815 LDA $EA62
402 = $0818 CMP $081D
403403 = $081B RTS
404404 = $081C .byte $BB
405405 = $081D .byte $0B