                              1         .area   _CODE
                              2 
                              3 ; This multiplication routine is similar to the one
                              4 ; from Rodnay Zaks, "Programming the Z80".
                              5 
   0000                       6 __muluchar_rrx_s::
   0000 21 02 00              7         ld      hl, #2
   0003 39                    8         add     hl, sp
   0004 5E                    9         ld      e, (hl)
   0005 23                   10         inc     hl
   0006 66                   11         ld      h, (hl)
   0007 2E 00                12         ld      l, #0
   0009 55                   13         ld      d, l
   000A 06 08                14         ld      b, #8
   000C                      15 muluchar_rrx_s_loop:
   000C 29                   16         add     hl, hl
   000D 30 01                17         jr      nc, muluchar_rrx_s_noadd
   000F 19                   18         add     hl, de
   0010                      19 muluchar_rrx_s_noadd:
   0010 10 FA                20         djnz    muluchar_rrx_s_loop
   0012 C9                   21         ret
                             22 
                             23 ;; Originally from GBDK by Pascal Felber.
                             24 
   0013                      25 __mulschar_rrx_s::
   0013 21 02 00             26         ld      hl,#2
   0016 39                   27         add     hl,sp
                             28 
   0017 5E                   29         ld      e,(hl)
   0018 23                   30         inc     hl
   0019 6E                   31         ld      l,(hl)
                             32 
                             33         ;; Fall through
   001A                      34 __mulschar_rrx_hds::
                             35         ;; Need to sign extend before going in.
   001A 4D                   36         ld      c,l
                             37 
   001B 7D                   38         ld      a,l
   001C 17                   39         rla
   001D 9F                   40         sbc     a,a
   001E 47                   41         ld      b,a
                             42 
   001F 7B                   43         ld      a,e
   0020 17                   44         rla
   0021 9F                   45         sbc     a,a
   0022 57                   46         ld      d,a
                             47 
   0023 C3r34s00             48         jp      .mul16
                             49 
   0026                      50 __mulint_rrx_s::
   0026 21 02 00             51         ld      hl,#2
   0029 39                   52         add     hl,sp
                             53 
   002A 5E                   54         ld      e,(hl)
   002B 23                   55         inc     hl
   002C 56                   56         ld      d,(hl)
   002D 23                   57         inc     hl
   002E 7E                   58         ld      a,(hl)
   002F 23                   59         inc     hl
   0030 66                   60         ld      h,(hl)
   0031 6F                   61         ld      l,a
                             62 
                             63         ;; Fall through
                             64 
   0032                      65 __muluchar_rrx_hds::
   0032                      66 __mulint_rrx_hds::
                             67 	;; Parameters:
                             68 	;;	HL, DE (left, right irrelivent)
   0032 44                   69 	ld	b,h
   0033 4D                   70 	ld	c,l
                             71 
                             72 	;; 16-bit multiplication
                             73 	;;
                             74 	;; Entry conditions
                             75 	;;   BC = multiplicand
                             76 	;;   DE = multiplier
                             77 	;;
                             78 	;; Exit conditions
                             79 	;;   DE = less significant word of product
                             80 	;;
                             81 	;; Register used: AF,BC,DE,HL
   0034                      82 .mul16:
   0034 21 00 00             83         ld      hl,#0
   0037 78                   84         ld      a,b
                             85         ; ld c,c
   0038 06 10                86         ld      b,#16
                             87 
                             88         ;; Optimise for the case when this side has 8 bits of data or
                             89         ;; less.  This is often the case with support address calls.
   003A B7                   90         or      a
   003B C2r41s00             91         jp      NZ,1$
                             92 
   003E 06 08                93         ld      b,#8
   0040 79                   94         ld      a,c
   0041                      95 1$:
                             96         ;; Taken from z88dk, which originally borrowed from the
                             97         ;; Spectrum rom.
   0041 29                   98         add     hl,hl
   0042 CB 11                99         rl      c
   0044 17                  100         rla                     ;DLE 27/11/98
   0045 30 01               101         jr      NC,2$
   0047 19                  102         add     hl,de
   0048                     103 2$:
   0048 10 F7               104         djnz    1$
   004A C9                  105         ret
