From 8c8c219d3e5bf4f2c0af74528de21ebe7783743b Mon Sep 17 00:00:00 2001
From: "B. Watson" <yalhcru@gmail.com>
Date: Fri, 21 May 2021 03:03:33 -0400
Subject: Save 5 bytes, now 8974

---
 console.s | 34 ++++++++++++++++++++++++++--------
 taipan.c  | 13 ++++++++-----
 2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/console.s b/console.s
index 70cffe3..3d43dc1 100644
--- a/console.s
+++ b/console.s
@@ -353,7 +353,7 @@ _set_orders:
 done1:
  rts
 
-; extern void __fastcall__ print_score_msg(long *scoreptr)
+; extern void __fastcall__ print_score_msg(long score)
 
 ; asm replacement for this C code:
 
@@ -363,13 +363,31 @@ done1:
 ;   else if(score < 100)
 ;      print_msg(M_land_based_job);
 
-; This turns out to actually waste 3 bytes: my code is the same
-; length as what cc65 generates, plus 3 bytes for the jsr to this
-; function. Ugh.
-
-;; .import _M_stay_on_shore, _M_land_based_job
-;; .export _print_score_msg
-;;_print_score_msg:
+ .import _M_stay_on_shore, _M_land_based_job
+ .export _print_score_msg
+_print_score_msg:
+ sta FR0
+ lda sreg+1              ; is MSB sign bit set?
+ bpl @notneg
+ lda #<_M_stay_on_shore  ; if so, print this message
+ ldx #>_M_stay_on_shore
+@pm:
+ jmp _print_msg
+@notneg:                 ; else MSB is positive. Is it non-zero?
+ bne done1               ; if non-zero, score is at least 2^24+1, no message
+ txa                     ; check bits 8-15...
+ ora sreg                ; ...and 16-23
+ bne done1               ; if either middle byte is non-zero, score>=256, no message
+ lda FR0                 ; here, the top 3 bytes are zero, so check the LSB.
+ cmp #99                 ; is it < 100?
+ bcs done1               ; if not, no message. or,
+ lda #<_M_land_based_job ; if so, print this message
+ ldx #>_M_land_based_job
+ bne @pm                 ; branch always (since message is not in zero page)
+
+;;
+;;
+;;
 ;; sta FR0
 ;; stx FR0+1
 ;; ldy #3
diff --git a/taipan.c b/taipan.c
index 70996e8..6e8ac49 100644
--- a/taipan.c
+++ b/taipan.c
@@ -1961,8 +1961,7 @@ void retire(void) {
    final_stats();
 }
 
-// This didn't work out (revisit someday?)
-// extern void __fastcall__ print_score_msg(long *scoreptr);
+extern void __fastcall__ print_score_msg(long score);
 
 long score_lim[] = {
 	50000L,
@@ -1990,7 +1989,7 @@ const char const *score_desc[] = {
 
 void final_stats(void) {
 	char i, unrated = 1;
-   int years = year - 1860;
+	int years = year - 1860;
 
 #ifdef BIGNUM
 	long score;
@@ -2074,6 +2073,7 @@ void final_stats(void) {
 	crlf();
 	rvs_off();
 
+	/* 8969 bytes with this stuff:
 	if(score < 0)
       // cputs("The crew has requested that you stay on\n"
 				// "shore for their safety!!\n\n");
@@ -2081,9 +2081,12 @@ void final_stats(void) {
 	else if(score < 100)
       // cputs("Have you considered a land based job?\n\n\n");
 		print_msg(M_land_based_job);
+		*/
 
-	// Tried to optimize the above stanza, failed to beat the compiler:
-	// print_score_msg(&score);
+	/* Optimized the above stanza, only saved 5 bytes (8874).
+		Compiled code is 45 bytes. Replacing with a function call costs
+		3 bytes, so the asm function has to be <42 bytes (it is). */
+	print_score_msg(score);
 
    // cputs("Your Rating:\n");
 	gotox0y(11);
-- 
cgit v1.2.3