Assembly Language Programming Inception

Introduction

Well I really wanted to write about assembly language programming, so here I am writing about it. I first came across the term assembly language when I was learning about Intel’s 8085 in my junior college. There we were introduced to this concept. When we talk with other humans we use sentences to communicate. Now these sentences are made up of words and the words from alphabets. One more thing is language, the letters are specific to the language being used. If I say ”ਤੁਹਾਡਾ ਨਾਮ ਕੀ ਹੈ” you would be able to understand this only if you know Punjabi language. So the interpretation is done by the brain in normal conversations. We come across translators who translate one language to another. Now suppose that I know only Punjabi and you know only English, then we would need a translator. This enables us to use the language we know without having to learn a new language. Now correlating this example to processors, processor only understands binary language i.e. 1’s and 0’s. We understand English(well since this is an international language I’m stating this) so we can’t write 11011110000 directly. We need some English like language for talking to the controller. This is where assembly language comes into picture. The instructions are given certain English like terms so that we humans can easily write programs and understand the written programs. Now each instruction in assembly language has an opcode which is nothing but a hex value. This hex or hexadecimal value is what the processor understands. When we were learning 8085, we used to do manual assembly. We wrote the code in assembly language, then using the opcode sheet wrote the corresponding opcodes down. Now these lines in hexadecimal numbers would be our code. This code was written into the FLASH/ROM memory. Since we manually used to write the hex values into the program memory this is called manual assembly. We talk of modern microcontrollers now. Today we get IDE’s and compilers and assemblers for all the modern microcontrollers be it msp430 or Arduino or Atmel. We write the program in English like language aka assembly language. The compiler will convert this assembly program into hexadecimal values and the assembler will burn this code in the flash memory. So the compiler generates the hex file (Now the name hex file makes sense,doesn’t it??!). This hex file is burnt into the flash memory i.e assembled into the memory  by the assembler. Now in this post I won’t be going into the details of how the program is burnt. We shall focus on how to write the code for now.

Starting with msp430 assembly language

Now that we know what assembly language is, let’s study msp430 assembly language. First thing is to learn the instructions. For that please refer the user guider  msp430x2xx for this. The section 3.4 is instruction set. The instructions are so beautifully explained there that I feel no need to explain each and everything again here. In order for anyone to write in assembly language one should know the instruction set thoroughly. Now I assume you know the instruction set from this point on!!!(If you do not understand any instruction you can comment on my blog. I’ll do the best I can to help you.) . Along with instruction set please read the addressing mode section as well. It is required as well.

CPU Registers

One of the most important resources of CPU is its registers. Most of the instructions involve them as one of the operands. Following picture shows all the registers.

registers

As we can see there are 16 registers in total. With 4 of them being special purpose registers and rest 12 general purpose registers. I’ll tell you about the PC,SP and SR for now.

PC

PC stands for program counter. This register contains the memory location of current instruction under execution. When the chip is reset,  program counter (PC) is loaded with address contained at reset vector location (0FFFEh). So when you reset the chip, [reset vector]–> PC. The reset vector address for mspx2xx is 0FFFEh. This feature is important while writing assembly language programs. I’ll talk about this when we write our first assembly language program.

SP

SP stands for stack pointer. Now this stack pointer points to top of stack. Stack is like a scratch pad where we store temporary data. We can push data onto stack. We use push and pop instructions while dealing with stack. Stack is nothing but RAM.

SR

SR stands for status register. This contains information about the result obtained after arithmetic or logical operations. Then SR bits are used to configure the low power modes in msp430. Also you can enable interrupts by setting GIE bit of SR. All in all SR is an important special register and has multiple applications.

Software

I’ll be using IAR Embedded Workbench for assembly language programs. The only reason for this is that the directives used in IAR are similar to KEIL which I learnt as a part of my curriculum. Directives are instructions used by the compiler to understand what to do with the code. You can use Code Composer Studio if you want. Note that for IAR the assembly language file is saved with .s43 extension unlike the usual .asm .

First Assembly Language program

;Name             : Manpreet Singh Minhas
;Date               : 23 Nov 2013
;Website         : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
\#include “msp430g2553.h”                                                            ;Include the header file
ORG 0FFFEh                                                                                     ;Reset vector address
DW MAIN
ORG 0C000h                                                                                     ;Flash memory starting address
MAIN:                    mov.w #WDTPW|WDTHOLD,&WDTCTL       ;Stop the watchdog timer
mov.b #BIT0|BIT6,&P1DIR                                ;Make P1.0 and P1.6 as output
mov.b #BIT0,&P1OUT                                        ;Make LED1 ON
REPEAT:             mov.w #0FFFFh,R7                                             ;Put value in counter for delay
UP:                       dec.w R7                                                               ;Decrement counter till 0
jnz UP                                                                    ;Repeat this 0FFFFh times
xor.b #BIT0|BIT6,&P1OUT                                 ;Toggle the LED1 and LED2
jmp REPEAT                                                        ;Infinite loop
END

Let’s understand what we have done in this program. I’ve written the hello world program again. This is LED blinking program in assembly language. I shall explain the basic directives now.

ORG

This directive tells the compiler that where it has to write the opcodes in memory. So ORG 0FFFFh tells the compiler that the next statement’s hex equivalent has to be stored in memory location 0FFFFh.

DW

This directive tells the compiler that we have defined a word and it is stored in memory which is given by ORG directive. So the code ORG 0FFFEh then DW 0C000h will store the word 0C000h in 0FFFEh memory location.

END

This directive tells the compiler that the code is over, whatever is written beyond this point is not relevant to the program.

Concept of LABELS

MAIN,REPEAT and UP are labels in the above program. Now these labels are nothing but the memory location of the statement where they are written. So MAIN is nothing but 0C000h, thus we could have very well written DW 0C000h instead of MAIN.

Numbers in assembly language

While writing numbers in assembly language you need to write 0 before a hexadecimal number starting with an alphabet. This is to make the compiler understand that the code is a number and not a label. I’ve developed a practice to write 0 before every number. But you can follow whatever you like. Thus FFFFh is not valid but 0FFFFh is valid. (and the 0 is zero and not the alphabet O)

Now we understand the code. The first two lines writes the starting address of the main program code at the reset vector address i.e. 0FFFEh. As I had discussed earlier that when the controller is reset the data at the reset vector memory location is loaded into the program counter, thus we write our main program starting address there. Then we write the main program in flash memory. Now for msp40g2553 flash starts at 0C000h, so I’ve written the main code there. You’ll have to change these addresses as per the data sheet of the controller you are using.

Then I’ve used simple mov instructions to initialize the watchdog timer and the ports. I’m giving this program for starting your assembly language coding. So you may want to use the timer section to give delay. I’ll write a code for that as well. Also if you have any doubts at all feel free to ask me. I’ll definitely share what I know.

Hope this post was informative. Thank you for reading this.

P.S:

Here are two more assembly language programs just for practice.

;Name             : Manpreet Singh Minhas
;Date                : 23 Nov 2013
;Website         : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
;This is a program to generate the well known Fibonacci series.
#include “msp430g2553.h”
ORG 0FFFEH                       ;RESET VECTOR
DW MAIN                        ; GOTO MAIN LABEL
ORG 0C000H
MAIN:        MOV.W #WDTPW|WDTHOLD,&WDTCTL    ; STOP THE WATCHDOG TIMER
MOV.W #03FFH,SP            ; INITIALIZE STACK POINTER
MOV.W #00H,R4            ; FIRST ELEMENT
MOV.W #01H,R5            ; SECOND ELEMENT
MOV.W #00H,R6            ; AUXILLARY POINTER
MOV.W #010D,R7            ; NUMBER OF ELEMENTS
MOV.W R4,0x200(R6)
INCD.W R6
MOV.W R5,0x200(R6)
UP:        INCD.W R6
CLRC
DADD.W R4,R5
MOV.W R5,0x200(R6)
DECD.W  R6
MOV.W 0x200(R6),R4
INCD.W R6
DEC.W R7
JNZ UP
JMP $
END

;Name             : Manpreet Singh Minhas
;Date                 : 23 Nov 2013
;Website        : https://learningmsp430.wordpress.com/
;Software        : IAR Embedded Workbench
;This program transfers data from one memory location to other.
#include “msp430g2553.h”
ORG 0FFFEH  ; This is the reset vector address.
DW MAIN
ORG 0C000H ; Starting of flash memory.
MAIN:   mov.w #WDTPW|WDTHOLD,WDTCTL ; stop the watchdog timer
mov.w #03ffh,SP            ; initialize the stack pointer to top of ram
mov.w #0200h,r4            ; initialize the destination reference pointer
mov.w #0e000h,r5           ; initialize the source pointer
mov.b #010d,r6
up:     mov.w @r5+,0(r4)
incd.w r4
dec.w r6
jnz up
jmp $
ORG 0E000H
DB “MANPREETSINGHMINHAS”
END

For both these you can use the IAR emulator. I’ll cover how to use that later.

Advertisements

3 thoughts on “Assembly Language Programming Inception

  1. Great, thank you for starting to explain how to code in Assembler. I have a question about the directives. I do not found something about directives in the User Guide. Is it a main element of Assembler programming without a Special relation to the MSP? I also do not understand the whole Memory Location Thing. I understand the Locations are adresses for the program to find the next step. Right? I am not good in finding the right words to explain my question. So i also understand i store a value in Register 1, and a value in Register 2, i can add the Values by ADD R1,R2 and the new value is stored in R2? What does the .b behind ADD mean? Can you make an example of the Memory usage like the example with your sister ringing at the door instead of checking the front door every Minute (This was your example of Interrupt usage instead of polling, a really good example i think).

    • I know that only one post on assembly language wont do any justice to it. Will write further parts later on. Directives are specific to the assembler so they are not dependent on msp430. You’ll have to see the IAR Workbench guide on their website or there is one for code composer studio on ti website. In that they have explained the directives and how to write in assembly language. The .w and .b refer to the register being a word or byte i.e. 16bit or 8bit. So when you refer any SFR that is 16bit in size you use .w after the instruction. And by default the operation is 16 bit. Instructions are very well explained in the user guide. I recommend that you read that part. MOV source,destination is the syntax (sort of) so the destination is the second operand and source is the first operand. I’ll try to make an example and write it.

  2. Pingback: Microcontroller Projects |

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s