;;;;;;;;;;;;;;;;;;;;;;MAKING COMPLEX RUSSIAN STRINGS

.org 0x8008f600


;InitProgram + First Hybrid Copy

    lui r2,0x8009
    sw r4,-0x30(r2)             ;;;Store start string addr
    lhu r3,0x0(r5)
    nop
    srl r2,r3,0xD               ;;;;Check 13th bit (1byte system !!!)
    bne r2,zero,isMyString      ;;if yes - jump to my prog. If no - init this
    lhu r3,0x0(r5)              ;load first 2-byte
    
    clear r2                    ;clear MAIN counter

    ;Standart 2-byte name
    addiu r4,2                  ;DestAddr + 2 (For my xx20 Command)
next2bytes:
    sb r3,0x0(r4)
    addiu r4,1                   ;add dest counter
    addiu r5,2                   ;add src counter
    addiu r2,1                   ;add char counter
    lhu r3,0x0(r5)              ;read next jap char
    nop
    srl r3,r3,0xC               ;Is it end of 2byte-string???
    bne r3,zero,saveLengthAndExit
    lhu r3,0x0(r5) 
    j next2bytes
    nop

saveLengthAndExit:
    ori r2,0x2000               ;add my command
    lui r3,0x8009                
    lw r3,-0x30(r3)             ;load str init address
    nop
    sh r2,0x0(r3)                ;save init counter +20 to dest addr (half)
    jr ra               ;exit
    move r2,r4   ;Duplicate for futher script


isMyString:
    lhu r3,0x0(r5)
    nop
    andi r3,r3,0xFF
    addiu r3,2              ;length to copy = charnum+2
nextMyByte:
    lbu r2,0x0(r5)
    nop
    sb r2,0x0(r4)
    addiu r4,1
    addiu r3,-1
    bne r3,zero,nextMyByte
    addiu r5,1
    jr ra               ;exit
    move r2,r4   ;Duplicate for futher script

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.org 0x8008f700                     ;Adding my text Program

   lhu r3,0x0(r5)          ;read src string length        
    nop
        ;NEED TO CHECK STRING FORMAT
    srl r2,r3,0xD               ;;;;Check 13th bit (1byte system !!!)   ;FOR TATSHUYA BOSS
    beq r2,zero,secondStrisJap
    nop

    lbu r2,0x0(r4)          ;read destination string length (1 byte)
    nop
    lbu r3,0x0(r5)          ;read src string length
    nop
    add r2,r3               ;Sum lengts of old and new strings
    sb r2,0x0(r4)           ;save new string length
    
    addiu r5,2            ;shift src +2 (len code)
    sub r2,r3
    addiu r4,2
    add r4,r2           ;shift destination - len+2 
    
nextMyByte2:
    lbu r2,0x0(r5)
    nop
    sb r2,0x0(r4)
    addiu r4,1
    addiu r3,-1
    bne r3,zero,nextMyByte
    addiu r5,1
    jr ra               ;exit
    move r2,r4   ;Duplicate for futher script


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
secondStrisJap:

    lbu r3,0x0(r4)          ;read dest string length
    addiu r4,2                  ;DestAddr + 2 (For my xx20 Command)
    add r4,r3             ;add dest counter = end of prev string

    lhu r3,0x0(r5)              ;load first 2-byte
    
    clear r2                    ;clear MAIN counter

    ;Standart 2-byte name
   
next2bytes2:
    sb r3,0x0(r4)
    addiu r4,1                   ;add dest counter
    addiu r5,2                   ;add src counter
    addiu r2,1                   ;add char counter
    lhu r3,0x0(r5)              ;read next jap char
    nop
    srl r3,r3,0xC               ;Is it end of 2byte-string???
    bne r3,zero,saveLengthAndExit2
    lhu r3,0x0(r5) 
    j next2bytes2
    nop

saveLengthAndExit2:
    lui r3,0x8009
    lw  r3,-0x30(r3)               
    nop
    lhu r3,0x0(r3)             ;load str prev length
    nop
    add r2,r3,r2                 ;main counter + prev string length

    ;ori r2,r3,0x2000               ;add my command  = xx20
    
   lui r3,0x8009
    lw  r3,-0x30(r3)               
    nop
    sh r2,0x0(r3)                ;save counter +20 to dest addr (half)
    jr ra               ;exit
    move r2,r4   ;Duplicate for futher script




    ;NAMEENTER SWAP ROUTINE


    .org 0x8008f800                     ;Adding my text Program
    ;r4 - destination
    ;r5 - source
    move r6,ra

    lui r5, 0x8008
    ori r5, 0x0a7c          ;Copy name to temp
    lui r4, 0x8008
    ori r4, 0xfa0e          ;dest+2
    jal 0x80015fe0                              ;TEMP - 80080a0e
    nop

    lui r5, 0x8008
    ori r5, 0x0ae4          ;Copy surname to name
    lui r4, 0x8008
    ori r4, 0x0a7e          ;dest+2
    jal 0x80015fe0
    nop

    lui r5, 0x8008
    ori r5, 0xfa0c          ;Copy temp to surname   ;TEMP - 80080a0c
    lui r4, 0x8008
    ori r4, 0x0ae6          ;dest+2
    jal 0x80015fe0
    nop

    move ra,r6
    li r4,0

    j 0x80017a9c                  ;EXIT
    