Simple Assembly Code to Implement STRCMP in MSP430
Here I am trying to implement the STRCMP function defined by Kernighan and Ritchie. Instead of returning the value zero for equal to or non-zero values is replaced by blinking green led if the two strings are same and else blinking the red led.
For a temporary purpose, as I am not familiar with the initial steps of the assembly code generation, first create a dummy C file just containing the main and opening and closing braces. Now compile the C file with -S option to generate the assembly code for MSP430. Now we can edit the ‘.s’ file.
As this is a simple learning process I am implementing the code with two predefined strings, in this case both are same ‘hjl’. The base address is stored at 0x260 and 0x240.
First the register r15 is loaded to memory address 0x260. 0x6a68 is the hexadecimal of ‘hj’ stored in little endian form. 2(r15) stores a byte 0x6c to the address 2+where r15 is loaded. clr.b clear the 4th byte from 0x260.
The same code is used to store the second string at location 0x240 into the register r14.
This will makes the two led pin p1.0 and p1.6 to out mode i.e. output mode. 0x0022 is the memory address for P1DIR. This instruction will move 41 to the memory address of P1DIR, hence we are using ‘&’.
This is the main comparison, i.e. whether the two strings are same character by character. @r14 will interpret the value in r14 as an address and try to get the value stored at that address. If the comparison fails it will jump to label l0 where the code to light the red led is present. If the comparison is true it will jump to label l1.
The first comparison is to check whether the end of string is reached. If reached, the two strings are same and control jump to label l2, where the steps needed to blink the green led is present. If end of string is not reached, both r14 and r15 are incremented. inc increments the value by one. After that the control return to the label loop.
If the comparison is a failure the control comes to label l0. The mov instruction is to blink the red led. 0x0021 is the memory address of P1OUT. Already we have made the pin p1.0 and p1.6 as output. In this case we are giving 1 to P1OUT thus making p1.0 to high and p1.6 remains low and red led blinks.
This will make p1.6 to high and p1.0 to low, thus blinking the green led.
In this example, as the two strings are same the green led blinks. We can also try different string by storing different string in r14 and r15.