Reputation: 33
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'
Upvotes: 1
Views: 99
Reputation: 33
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
Upvotes: 1