Assembler How work with index in db Beginner

77 Views Asked by At

I have a one problem, I don't know how to get step by step index from db:C here is my code:

    Morse:
                        mov  DPTR, #Text     ; Point to the beginning of the text
                            mov  A, R0 ;offset to acc
    
    NextChar:
                        movc A, @A+DPTR            ; Load a character from the text into register A
                            cjne A, #2Eh, EndProgram   ;check for dot
                            cjne A, #20h, WordSpace    ;check for space
                        cjne A, #00h, CharLoop     ; Check if the character is not null (end of text)
                        jmp  EndProgram            ; Otherwise, move to the next character in the text  !!!!!!!!HOW TO CHECK IF IT NULL!!!!!!!!!!!

    CharLoop:
                            orl  A, #20h
                            clr  C ;clear C flag after orl
                            subb A, #61h
                            mov  DPTR, #ASCII_TO_MORSE_TABLE
                            movc A, @A+DPTR
                            ;mov  R4, A     ; R4 = Code MORSE
                            inc  R0;             ; counter chars at Text
                            mov  R2, #00h  ; counter dot\dash in char
                            jmp DotORDash

  DotORDash:
                            ; here at A,B,R4 I have for example "--.-!"
                            ; How here load to A just first char from, A,B,R4?
                            movc A, @A+DPTR
                            inc R2 ; "which char morse ++"
                            cjne A, #21h, Morse; if r4 == "!" jmp Morse
                            cjne A, #2Dh, DASH; if r4 == "-" call DASH
                            cjne A, #2Eh, DOT; if r4 == "." call DOT
                            jmp Morse

ASCII_TO_MORSE_TABLE:db     '.-!', '-...!', '-.-.!', '-..!',    '.!', '..-.!', '--.!',  '....!', '..!', '.---!', '-.-!', '.-..!',   '--!', '-.!', '---!', '.--.!', '--.-!', '.-.!', '...!', '-!', '..-!', '...-!', '.--!', '-..-!', '-.--!', '--..!', '-----!', '.----!', '..---!', '...--!', '....-!', '.....!', '-....!', '--...!', '---..!', ' ----.!' 
                                                    ; | a  | |  b   | |  c  |    |  d    |  |e|   |  f  |  | g   |  |  h   | | i  | |  j   | |  k  | |  l  |  | m  | | n  | |  o  | |  p  |  |  q  |  |  r |  |  s  | | t | |  u |  |  v  |  |  w  | |  x  |  |  y  |  |  z  |  |   0  |  |   1  |  |   2  |  |   3  |   |  4  |  |   5  |  |   6  |  |   7  |   |   8  |  |   9  |

So the main problem for me its how at DotORDash get step by step chars, for example I have at B,A,R4 this value: '.-!', how I can at DotORDash get step by step these values?

1

There are 1 best solutions below

0
Peter Plesník On

The code is written on the fly, it has not been compiled, its task is to show the algorithm for finding the beginning of the morse code for any alpha character.

First of all, you need to understand how the cjne instruction works. Subsequently, it is necessary to realize that this is not about any database manipulations (please remove the database and indexing tags), but about linear searching of text strings with using a pointer.

The rest should be clear from the source text itself.

Morse:
                        mov  DPTR, #Text     ; Point to the beginning of the text
                        mov  A, R0           ;offset to acc
    
NextChar:
                        movc A, @A+DPTR      ; Load a character from the text into register A
                        inc  r0
                        cjne A, #'.', nextTest01   ;check for dot
                        rjmp endProgram
nextTest01:             cjne A, #' ', nextTest02
                        rjmp WordSpace    ;check for space
notAlpha:               jmp Morse
nextTest02:             jz   EndProgram   ; Check if the character is not null (end of text)

;A is character to decode 
                        mov r3,a          ;R3 temp register
                        clr c
                        subb a,#'A'
                        jc notAlpha
                        mov a,r3
                        setb c
                        subb a,#'z'
                        jnc notAplha
                        mov a,r3
;A is alphabet character from range 'A - z' (not tested special chars)
                        orl  A, #20h      ;convert to lowercase
                        clr  C            ;clear C flag after orl
                        subb A, #'a'      ;calculate the sequence number of the character
                        mov  DPTR, #ASCII_TO_MORSE_TABLE ;set pointer to first char ('a')
                        jz  morseCode
                        mov r3,a
;find start morse code for next char
;increasing pointer in loop until separate char '!' is not find
noSeparateChar:         clr a
                        movc A, @A+DPTR
                        inc dptr                   ;move to next char
                        cjne a,#'!',noSeparateChar ;repeat until separator is not find
                        djnz r3, noSeparateChar    ;repeat until find requested char

morseCode:
; DPTR now point to start of morse code sequence

                        clr a
                        movc A, @A+DPTR
                        inc dptr ;move pointer to next morse char
                        cjne A, #'!', nextTest03
                        rjmp Morse ; if '!' then process next char of message
nextTest03:             cjne A, #'-', DOT ;if dot make somethnig (no others chars as '.' are enabled)
                        jmp DASH ; if '-' make something

;table must not contain digit.
;you must create separate table for digits and make similar search as for chars
ASCII_TO_MORSE_TABLE:db     '.-!', '-...!', '-.-.!', '-..!',    '.!', '..-.!', '--.!',  '....!', '..!', '.---!', '-.-!', '.-..!',   '--!', '-.!', '---!', '.--.!', '--.-!', '.-.!', '...!', '-!', '..-!', '...-!', '.--!', '-..-!', '-.--!', '--..!' 
                                                    ; | a  | |  b   | |  c  |    |  d    |  |e|   |  f  |  | g   |  |  h   | | i  | |  j   | |  k  | |  l  |  | m  | | n  | |  o  | |  p  |  |  q  |  |  r |  |  s  | | t | |  u |  |  v  |  |  w  | |  x  |  |  y  |  |  z  |