comparing words in assembly code and other questions

  Kiến thức lập trình
preset.inc

#define BAUDRATE 57600
#define UBRR0 F_CPU/(BAUDRATE*16)-1
#define LF 0x0A
#define CR 0x0D
#define EOS 0x00 ; (end of string)
#define BS 0x7F
#define SetGREEN 0x1b,"[0;32m"
#define ClrCOLOR 0x1b,"[0m"

.macro SETSP ; Usage : SETSP RAMEND ;
    ldi temp,low(@0) ; @0 indicates first parameter
    out SPL,temp;
    ldi temp,high(@0);
    out SPH, temp;
.endmacro

.macro SETXY
    ldi @0H, high(@1)
    ldi @0L, low(@1)
.endmacro

.macro SETZ ; Usage : SETZ SegData ;
    ldi ZH, HIGH(2*@0)
    ldi ZL, LOW(2*@0)
.endmacro

.macro SET_Register_R0R1R2
    clr r0
    ldi temp, 0x01
    mov r1, temp
    ldi temp, 0xff
    mov r2, temp
.endmacro

.macro PUTC
    ldi r24, @0
    rcall PutChar
.endmacro

.macro PRINT
    SETZ @0
    rcall PutString
.endmacro

.macro ADDI16
    subi @0L, low(-@1)
    sbci @0H, high(-@1)
.endmacro
UARTio.asm

GetString:
    rcall Getchar
    cpi r24, CR
    breq Getend
    rcall Putchar
    st Y+, r24
    rjmp GetString

Getend:
    ldi r24, EOS
    st Y+, r24
    ret

PutString:
    lpm r24, Z+
    tst r24
    breq Putend
    rcall PutChar
    rjmp PutString

Putend:
    ret

PutStringD:
    ld r24, Y+
    tst r24
    breq Putend2
    rcall PutChar
    rjmp PutStringD

Putend2:
    ret

Uart_Init:
    ldi temp, HIGH(UBRR0);
    sts UBRR0H, temp ;
    ldi temp, LOW(UBRR0);
    out UBRR0L, temp ; Set baudrate
    ldi temp, $18 ;
    out UCSR0B, temp; TX & RX enable without interrupt
    ldi temp, $06 ; Data8 Bit, 1 Stop Bit, No parity
    sts UCSR0C, temp;
    ret ;

PutChar:
    // Check if r24 == CR or not
    cpi r24, CR ;
    brne PutData ;

InsertLF:
    push r24 // backup r24 for normalPut
    ldi r24, LF
    rcall PutChar
    pop r24 // restore r24

PutData:
    sbis UCSR0A, UDRE0;
    rjmp PutData;
    out UDR0, r24;
    ret ;

GetChar:
    sbis UCSR0A, RXC0 ;
    rjmp GetChar ;
    // Get the received data from buffer
    in r24, UDR0 ;
    ret
main.asm

#include "preset.inc"

.CSEG
    .ORG 0x0000
    jmp RESET

    .ORG 0x0046;

RESET:
    SETSP RAMEND
    SET_Register_R0R1R2

    rcall Uart_Init
    rcall PrintTitle

forever:
    rcall PrintCursor
    rcall ReadCmd
    rcall CmdInterprete
    rjmp forever

PrintTitle:
    PRINT BuildTime
    PRINT Title
    ret

PrintCursor:
    PRINT CMDcursor
    ret

ReadCmd:
    SETXY Y, buffer
    rcall GetString
    PUTC CR
    ret

CmdInterprete:
    SETXY Y, buffer
    SETZ CMDList

    call StrCmp
    tst r24
    breq CMD_HELP

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_LED

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_SEG1

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_SEGN

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_PHONE

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_BUZ

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_PHOTO

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_TEMP

    ADDI16 Z, 6
    call StrCmp
    tst r24
    breq CMD_PROJ

CMD_Wrong:
    PRINT BAD_MSG
    SETXY Y, buffer
    rcall PutStringD
    PUTC '!'
    PUTC CR
    ret

CMD_HELP:
    PRINT HELP_MSG
    ret
CMD_LED:
    PRINT LED_MSG
    ret
CMD_SEG1:
    PRINT SEG1_MSG
    ret
CMD_SEGN:
    PRINT SEGN_MSG
    ret
CMD_PHONE:
    PRINT PHONE_MSG
    ret
CMD_BUZ:
    PRINT BUZ_MSG
    ret
CMD_PHOTO:
    PRINT PHOTO_MSG
    ret
CMD_TEMP:
    PRINT TEMP_MSG
    ret
CMD_PROJ:
    PRINT PROJ_MSG
    ret

StrCmp:
    push r25 ; register internally used
    push r26
    push r28 ; Y,Z register
    push r29
    push r30
    push r31
    
StrCmpLoop:
    ld r25, Y+
    ld r26, Z+
    tst r26
    breq StrCmp_Same ; if EOS, end
    cp r25, r26
    brne StrCmp_Dif ; if different, end
    rjmp StrCmpLoop

StrCmp_Same:
    clr r24
    rjmp StrCmp_End

StrCmp_Dif:
    mov r24, r1

StrCmp_End:
    pop r31
    pop r30
    pop r29
    pop r28
    pop r26
    pop r25
    ret
    
HELP_MSG:
    .db "Here is HELP Command.....",CR,EOS,EOS
LED_MSG:
    .db "Here is LED Command.....",CR,EOS
SEG1_MSG:
    .db "Here is SEG1 Command.....",CR,EOS,EOS
SEGN_MSG:
    .db "Here is SEGN Command.....",CR,EOS,EOS
PHONE_MSG:
    .db "Here is PHONE Command.....",CR,EOS
BUZ_MSG:
    .db "Here is BUZ Command.....",CR,EOS
PHOTO_MSG:
    .db "Here is PHOTO Command.....",CR,EOS
TEMP_MSG:
    .db "Here is TEMP Command.....",CR,EOS,EOS
PROJ_MSG:
    .db "Here is PROJ Command.....",CR,EOS,EOS
BAD_MSG:
    .db "Wrong Command....."

CMDcursor:
    .db SetGREEN,"CMD:>> ",ClrCOLOR,EOS, EOS ;

BuildTime:
    .db "Built on", __DATE__," ", __TIME__, CR, EOS

Title:
    .db "+=====================================================================+",CR ;
    .db "|  This is an AVR Board Test                                          |",CR ;
    .db "|  Programmed by me                                                   |",CR ;
    .db "|  Following commands are provided                                    |",CR ;
    .db "+=====================================================================+",CR ;
    .db "|  help : Display all supported command                               |",CR ;
    .db "|  led  : LED Test                                                    |",CR ;
    .db "|  seg1 : Single Segment Display Test                                 |",CR ;
    .db "|  segn : Multiple Segment Display Test                               |",CR ;
    .db "|  phone: Phone Number Display Test                                   |",CR ;
    .db "|  buz  : Buzzer Test                                                 |",CR ;
    .db "|  photo: Photo Sensor test                                           |",CR ;
    .db "|  temp : Temperature Sensor Test                                     |",CR ;
    .db "|  proj : Your Project Test                                           |",CR ;
    .db "+=====================================================================+",CR ;
    .db " Pressing CTRL-C terminates selected test !!    ",CR, EOS ;

CMDList:
    .db "help", EOS, EOS;
    .db "led",EOS, EOS, EOS;
    .db "seg1", EOS, EOS;
    .db "segn", EOS, EOS;
    .db "phone" ,EOS;
    .db "buz",EOS, EOS, EOS;
    .db "photo" ,EOS;
    .db "temp", EOS ,EOS;
    .db "proj", EOS, EOS;
    
#include "UARTio.asm"

.DSEG
buffer:
    .byte 80

main code supposed to get user’s command like ‘help’, ‘led’, ‘proj’… through putty,
then it prints ‘here is ‘xxx’ command’ depends on the command.

But there are 2 problems.
whenever i try a command(not mistyped) on putty, it just saying like this, “Wrong Command…..CMD:>> xxx!”
I think there is an error in my subroutine ‘StrCmp’,cause whatever I try, it just goes to the ‘CMD_Wrong’ subroutine.
And that ‘CMD:>> ‘ in “Wrong Command…..CMD:>> xxx!” is a problem too.
I intended it to say “wrong command…..xxx!”, but it adds this useless “CMD:>>” in the middle of a sentence.enter image description here

I’ve been struggling with this for a day, but I cant find what’s wrong.
I used gpt4, claude 3 opus to debug, but they cannot find an error in my code. and me neither…
can anyone help me doing this?..

New contributor

jeongnam Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT