Page 1 of 3

Falco's MSP430 (and now x86) Assembly Programs

Posted: Wed Sep 16, 2009 5:21 pm
by Falco Girgis
This topic is for all of my projects/programs/assignments for my embedded processors class (CPE323). This topic will be especially interesting for those of you who tell yourself "omg, I would love to learn assembly."

The MSP430 is a tiny, lightweight, embedded processor with 64K of addressable RAM and a clock of 8-16Mhz. It is roughly equivalent to a Sega Genesis processor spec wise.

I will comment the hell out of my programs and offer any information to anybody interested. You can also download a simulator/emulator and IDE (and write/run my code or write your own) for the MSP430 from Texas Instrument's website: http://focus.ti.com/docs/toolsw/folders ... start.html

I was given roughly two weeks to complete this assignment, but being the lazy ass that I am, I put it off until the day before it was due. It took me about 2 hours to complete.

I think assembly programming is a great way to expand your programming skills. It forces you to think outside of the strictly C++ way that your brain has adapted to solving problems. I always feel smarter after writing something in ASM.

Code: Select all

/*
Falco Girgis
CPE323 Lab Section 2
Project #2

Program takes a string "inStr" and creates "outStr" which lowercases
every letter that isn't the first letter in the sentence. The total number
of characters in inStr are written to port P1, and the total number of 
changed characters are outputted to port P2.

inStr - allocated somewhere in RAM
outStr - starts at byte following inStr in RAM
R4 - pointer to current character in inStr
R5 - pointer to current address of outStr
R6 - current character
R7 - beginning of sentence flag
R14 - counter for total number of characters
R15 - counter for total number of changes
*/

#include "msp430.h"            

        NAME    main                    ; module name
        PUBLIC  main                    ; make main accessible from outside module
        
        ORG     0FFFEh
        DC16    init                    ; set reset vector to 'init' label

        RSEG    CSTACK                  ; pre-declaration of segment CSTACK
        RSEG    CODE                    ; place program in 'CODE' segment

init:   MOV     #SFE(CSTACK), SP        ; initialize stack

main:   NOP                             ; main entry point
        MOV.W   #WDTPW+WDTHOLD,&WDTCTL  ; stop watchdog timer
        BIS.B   #0FFh,&P1DIR            ; configure P1.x output
        BIS.B   #0FFh,&P2DIR            ; configure P2.x output
        
        MOV     #inStr, R4              ; move start addr of inStr to R4
        MOV     #outStr, R5             ; move start addr of outStr to R5
        CLR     R14                     ; clear character counter
        CLR     R15                     ; clear change counter
        CLR     R7                      ; clear "beginning of sentence" flag
gchar:  MOV.B   @R4+, R6                ; put char in R6 and increase string pointer
        CMP     #0,R6                   ; check if curChar is null
        JEQ     done                    ; if so, jump to done
        INC     R14                     ; add one to character count
        ; CHECK LOWER - checks if things are NOT lowercase letters
chklwr: CMP     #'[', R6                ; Upper bound of lowercase letter
        JGE     chkPnc                  ; not in range, check if punctuation.    
        CMP     #'A', R6                ; lower bound of lowercase letter
        JL      chkPnc                  ; not in range, check if punctuation
        CMP.B   #1, R14                 ; check if first char in string
        JEQ     noPunc                  ; if yes, skip this letter
        CMP.B   #1, R7                  ; check if beginning of sentence
        JEQ     noPunc                  ; if yes, skip this letter
        ; TO LOWER - sets curChar to be a lowercase letter
tolowr:
        ADD     #32, R6                 ; add the ascii offset to uppercase letter
        INC     R15                     ; increment change counter
        ; OUT CHAR - Outputs character
outCh:  MOV.B   R6, 0(R5)               ; write curChar to RAM
        INC     R5                      ; increment outStr pointer
        JMP     gchar                   ; get next character
        
punct:  MOV     #1, R7                  ; set beginning of sentence flag
        JMP     outCh                   ; output character
        
noPunc: CLR     R7;                     ; clear flag
        JMP     outCh;                  ; output character
        
chkPnc: CMP.B   #'!', R6                ; check if char == '!'
        JEQ     punct                   ; if yes, get next char
        CMP.B   #'.', R6                ; check if char == '.'
        JEQ     punct                   ; if yes, get next char
        JMP     outCh

done:   
        MOV.B   0, R5                 ; append NULL to outStr
        MOV.B   R14, &P1OUT           ; store total number of characters
        MOV.B   R15, &P2OUT           ; store total number of changes

        JMP     $                        ; jump to itself so that the program NEVER STOPS EXECUTING AND IDLES

inStr   DB      "WELCOME to Cpe323. YOURS MSP430!!!!.. SUCK. ME. RAAAwwwW.W" ; the string is placed on the stack
outStr  DB      0  ; define the starting address for our outStr

        END
/flex

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 6:17 pm
by logank9
Good work on this. I don't think that I will ever dare to code in assembly, it looks confusing as hell from what I've seen so far.

Omigawd that's a lot of comments! :shock:

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 6:29 pm
by JaxDragon
I think there's more explanations than code ;) I really don't like assembly. It's too low-level for me. And assembly has too many abbreviated functions.

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 6:53 pm
by Falco Girgis
It doesn't have too many abbreviated functions. They're opcodes, not functions. Your entire list of operations on the MSP430 is approximately 23. If you're using 23 different things over and over and over, you would want them to be as small as possible also. ;)

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 6:57 pm
by JaxDragon
GyroVorbis wrote:It doesn't have too many abbreviated functions. They're opcodes, not functions. Your entire list of operations on the MSP430 is approximately 23. If you're using 23 different things over and over and over, you would want them to be as small as possible also. ;)
See? I totally do not understand assembly at all. I suppose I'll leave it to the pro's and continue to be a cute, and cuddly, C++ coder.

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 7:25 pm
by hurstshifter
JaxDragon wrote: See? I totally do not understand assembly at all. I suppose I'll leave it to the pro's and continue to be a cute, and cuddly, C++ coder.
+1

I'll leave the ASM to Falco :worship:

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 7:43 pm
by Bludklok
ASM scares me... but I know I want to try it someday.

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 8:03 pm
by avansc
i love how asm programs look the same in college. with uber comments. and i love that they are pretty much the same. this was the same one i had to do.

btw. there are a few things you are doing that you dont have to. you can make your program about 20 percent smaller.
some times you have to looks what you have.... and sometimes you have to check what you dont...
i'll let you maul over it and if you want i'll show you a few tricks..

ps: make use of the fact that its a linear execution. ordering of functions can take half the jumps needed out..

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 8:29 pm
by davidthefat
You know whats more hardcore than ASM? Writing a whole program in Binary... didnt Bill gates do that? LOL or make a PC from scratch... literally with transiters and stuff

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 9:03 pm
by avansc
davidthefat wrote:You know whats more hardcore than ASM? Writing a whole program in Binary... didnt Bill gates do that? LOL or make a PC from scratch... literally with transiters and stuff
:shock:

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 9:27 pm
by Falco Girgis
davidthefat wrote:You know whats more hardcore than ASM? Writing a whole program in Binary... didnt Bill gates do that? LOL or make a PC from scratch... literally with transiters and stuff
. . .

And you must be 12.



Furthermore, if you know how the opcode is encoded, writing a program in binary isn't more hardcore--just more tedious. And stupid.

Re: Falco's MSP430 Assembly Programs

Posted: Wed Sep 16, 2009 9:35 pm
by Bakkon
davidthefat wrote:You know whats more hardcore than ASM? Writing a whole program in Binary... didnt Bill gates do that? LOL or make a PC from scratch... literally with transiters and stuff
Do you ever think before you post?

Re: Falco's MSP430 Assembly Programs

Posted: Thu Sep 17, 2009 2:00 am
by K-Bal
davidthefat wrote:You know whats more hardcore than ASM? Writing a whole program in Binary... didnt Bill gates do that?
Steve Wozniak programmed a floppy disk driver this way, some hours before a presentation. Just like Falco said, if you know the ops it's just stupid work.

Re: Falco's MSP430 Assembly Programs

Posted: Thu Sep 17, 2009 2:29 am
by short
I like assembly, however I did it on the x86 processor last year. End of the semester program was to write a recursive maze problem (made out of # signs), when you write recursion is assembly it forces you to understand how local variables are pushed onto the stack and how they go out of scope. ie you learn exactly why that 4bit pointer you just referenced that is now out of scope sometimes works and sometimes does not work on a low level. I'm probably yammering about something you all know pretty well, but it's the single thing that I will remember most from that class)

Get the chance to learn it if you guys can who say they want to stay with c++. I could be out of my mind, but when you know assembly and you go to write C code I can visualize the underlying assembly code if I try too.

The one thing that can go to hell is inline assembly. Kidding, it has its uses. ie at someone else's computer. :lol:

Re: Falco's MSP430 Assembly Programs

Posted: Thu Sep 17, 2009 5:22 am
by programmerinprogress
Those instructions kind of remind me of the ASM when I did that brief spell of assembly on an ACORN system (1 - 2 days in my computing class) Iove it when the registers are given simple names like R1, R2, and also the mnemonics are fairly intuitive, i've looked at x86 assembly before and it just seems incredibly ambiguous unless you actually know how to use it well.

I've always had an interest in the lower-level stuff, and I think i'll probably move in to it in my spare time when i'm all settled in uni and i've managed to finish all of my C++/SDL projects... well uni does last 4 years so I could surprise you :lol: