0

I'm very new with assembly, I'm trying to build a character, word and spaces counter with Easy68k. I'm getting the user input, then looping through the characters, adding to the counter and trying to output the result, but all I could do was to output the same input a user enters, so I'm stuck. What am I doing wrong?

CR  EQU       $0D
LF  EQU       $0A

START: ORG    $1000

* Cargar dialogo en direccion de memoria
    LEA DIALOG,A1
   
* Mostrar prompt
    MOVE #14,D0
    TRAP #15

* Leer input de usuario
    MOVE #2,D0 ;lee el input
    MOVE D0,A0
    TRAP #15
   
* Enseña input de usuario
    MOVE #14,D0
    MOVE A1,A0
    TRAP #15


LOOP:
    TST.B   (A0)+        ; Carga el sig. caractetr
    BEQ     FINISHED     ; If si termina en 0, se termina el programa
    CMP.B   #32, D1      ; Checa si caracter es espacio
    BEQ     IS_SPACE     ; Si es, vamos a subrutina COUNTSPACE
    ADDQ    #1,D1           
    BRA     INCREMENT

IS_SPACE:
    ADDQ #1,(SPACES)
    BRA INCREMENT
    
INCREMENT:
    ADDQ #1,(CHARS)
    BRA LOOP

FINISHED:
    MOVE.B (SPACES),D1
    MOVE #14,D0
    TRAP #15

    SIMHALT


DIALOG: DC.B    'ESCRIBE ALGO: ',CR,LF,0  
CHARS: DC.B 0

Tried reading documentation and the simulator's examples, and repositories. Tried getting the input from user using task #2 I/O. But all I could do was to output the same input a user enters i.e. 'hello world', output: 'hello world'

expected: 'Characters: 11' 'Words:2' 'Spaces:1'

Anatheme
  • 13
  • 3
  • Well, why are you using task 14 (_display NULL-terminated string at `(A1)`_) at `FINISHED`? Shouldn't you be using e.g. task 3 (_display signed number in `D1`_) ? – Michael Aug 10 '23 at 14:09
  • Your usage of `D1` makes no sense. – Erik Eidt Aug 10 '23 at 15:04
  • @Michael, I've read examples that have that sequence, so I assumed it has to be done with all instructions that I wanted to be printed. I find the documentation and examples insufficient, so I'm kinda lost. – Anatheme Aug 10 '23 at 16:30
  • Well, How can I make it make sense, @ErikEidt? I'm trying to understand – Anatheme Aug 10 '23 at 16:31
  • `D1` is register storage,. That code is consulting `D1` via `cmp` but doesn't put anything there worth consulting. – Erik Eidt Aug 10 '23 at 16:38
  • The examples you looked at probably only printed strings then. You want to print an integer, so that would be done with a different trap task. See http://www.easy68k.com/QuickStart/TrapTasks.htm – Michael Aug 10 '23 at 17:24
  • According to the Help manual #2,D0, will get the keyboard input and Store it in A1. Now I'm doing this in my code (not yet posted) * Leer input de usuario MOVE #2,D0 ;lee el input TRAP #15 MOVE A1,A0 And Task 3 says that the number should be in D1, or at least that's what I'm understanding FINISHED: MOVE (SPACES),D1 MOVE #3,D0 TRAP #15 – Anatheme Aug 10 '23 at 19:00
  • Note that it is your responsibility to assign correct values to all parameters before executing a trap task. E.g., before executing trap task 2, you need to put the address where you want the characters to be stored into `A1`. – Michael Aug 11 '23 at 08:08
  • I think I got it, Thanks – Anatheme Aug 11 '23 at 15:10

1 Answers1

0

I want to share my code. It leaves some room for improvement, but Ì think this is the right direction. Thanks for your suggestions

CR  EQU       $0D
LF  EQU       $0A

START: ORG    $1000
* Cargar dialogo en direccion de memoria
    LEA DIALOG,A1
    MOVE #0,D2 ;D2 es flag para contar caracteres
    MOVE #0,D3 ; D3 para contar espacios
    MOVE #0,D4 ; D3 para contar palabras
* Mostrar prompt
    MOVE #14,D0
    TRAP #15
* Leer input de usuario
    MOVE #2,D0 ;lee el input
    TRAP #15

    MOVE A1,A0

LOOP:
    TST.B   (A0)+        ; Carga el sig. caractetr
    BEQ     INCREMENTSP     ;Ejecuta INCREMENTSP, para incrementar espacios
    BRA     INCREMENT ;Ejecuta INCREMENT, para incrementar caracteres


INCREMENTSP:
    CMP.W #$32,A0     ; Checa si caracter es espacio
    ADDQ #1,D3        ; Suma 1 al contador  
    BRA WORDSCOUNT    ; Ejecuta WORDSCOUNT
    BNE FINISHED
INCREMENT:
    ADDQ #1,D2          ; Incrementa contador de caracteres
    BRA LOOP            ; Ejecuta Subrutina LOOP, el ciclo
WORDSCOUNT:            ;Incrementa contador de palabras agregando 1 al total de esoacios 
    MOVE D3,D4  
    ADDQ #1,D4
FINISHED:              ; Ejecuta subrutinas para mostrar el numero de caracteres
    BRA SHOW_CHARS
    BRA SHOW_SPACES
    BRA SHOW_WORDS

    SIMHALT
SHOW_CHARS:
    LEA CHARS,A1 ;Muestra texto caracteres
    MOVE #14,D0
    TRAP #15

    MOVE D2,D1  ; Muestra num de caracteres
    MOVE #3,D0
    TRAP #15
SHOW_SPACES:
    LEA SPACES,A1 ;Muestra texto espacios
    MOVE #14,D0
    TRAP #15

    MOVE D3,D1 ; Muestra num de espacios
    MOVE #3,D0
    TRAP #15
SHOW_WORDS:
    LEA WORDS,A1 ;Muestra texto palabras
    MOVE #14,D0
    TRAP #15

    MOVE D4,D1  ; Muestra num de palabras
    MOVE #3,D0
    TRAP #15

*Mensajes
DIALOG: DC.W    'ESCRIBE ALGO: ',CR,LF,0
CHARS: DC.B 'CARACTERES: ',0, LF,CR
SPACES: DC.B ' ESPACIOS: ',0, LF,CR
WORDS: DC.B ' PALABRAS: ',0, LF,CR

        END     START
  
Anatheme
  • 13
  • 3