Falco's MSP430 (and now x86) Assembly Programs

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Falco's MSP430 (and now x86) Assembly Programs

Post 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
logank9
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Jul 27, 2009 1:54 pm

Re: Falco's MSP430 Assembly Programs

Post 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:
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Falco's MSP430 Assembly Programs

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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. ;)
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Falco's MSP430 Assembly Programs

Post 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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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:
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bludklok
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Tue Apr 14, 2009 1:31 am
Current Project: EnigmaCore
Favorite Gaming Platforms: PC, N64, Playstation1, Playstation2
Programming Language of Choice: C++
Location: New Jersey
Contact:

Re: Falco's MSP430 Assembly Programs

Post by Bludklok »

ASM scares me... but I know I want to try it someday.
Youtube
Website
Current project: Enigma Core
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Falco's MSP430 Assembly Programs

Post 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..
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Falco's MSP430 Assembly Programs

Post 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:
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Falco's MSP430 Assembly Programs

Post 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?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Falco's MSP430 Assembly Programs

Post 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:
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Falco's MSP430 Assembly Programs

Post 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:
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply