Posts Tagged ‘Assembly’

AVR Assembly on Mac OS X – Your First Program

Posted in Programming on December 18th, 2010 by Simon Connah – Be the first to comment

In the past I wrote an article about writing assembly programs for AVR devices on the Mac but missed a couple of important points.

The major problem with my original article was that I was passing everything through the C compiler rather than just using avr-as as one should. The other problem was that I was using the libc headers to get the names of the specific ports. The better solution is to take the include files that Atmel supply with AVR Studio 4 on the PC and just make a couple of small changes to make them work using avr-as. The include file I use for the ATmega168 can be found here.

By using this approach there is basically no difference between the assembly that you would use in AVR Studio 4 and that you can use on your Mac. This makes the entire development process extremely easy.

So lets list the source code of our updated LEDon.S file from the original article:

.include "/home/dev/m168.h"
 
	rjmp init
 
init:
 
	ser r16
	out DDRB, r16
	out DDRD, r16
 
	clr r16
 
	out PORTB, r16
	out PORTD, r16
 
	.global main
 
main:
 
	sbi PORTB, 0
	rjmp main

it should look pretty similar to the original.

The only real difference comes when you want to assemble it and produce a hex file which you then upload with AVRdude (as documented here). So you do that with these commands (assuming you have saved the above file as ledon.S and that the header file I provided is stored in /home/dev – if it is different change the include line to point to the correct path):

avr-as -mmcu=atmega168 -o ledon.out ledon.S
avr-objcopy -O ihex ledon.out ledon.hex

this should result in a nice ledon.hex file.

Now that you can use the “raw” assembler you can just read the data sheets and the AVR instruction reference and use the port names as is rather than having to use the rather stupid C macros that AVR libc provides. Obviously this technique is completely cross platform and will work on any UNIX based machine, not just Macs.

Creating, Compiling and Linking Your First AVR Assembly Program on Mac OS X

Posted in Programming on April 11th, 2010 by Simon Connah – Be the first to comment

Update 3: Although this article is now obsolete (see update 2 below) I thought I’d at least make sure the code blocks display properly using the new syntax highlighting system in use.

Update 2: I have rewritten this article here. If you want to do AVR programming in assembly on Mac OS X then read that article. This one is just kept around for historical reasons and to list another option if you want to do it a different (albeit harder) way.

Update: After a little bit more careful study of the AVR libc library I realised that you can just make use of the macros it contains in order to access ports etc without having any ugly defines.

I struggled to get up to speed with creating my first assembly program for my AVR device. The problem is that the tutorials and information available either target C for the AVR GCC tool chain (doesn’t help me much as I want to use assembly) or target assembly but using another assembler. There does not seem to be any specific information that I could get to work. I hope that this article will help if you find yourself in the same position.

So the first thing you need to do is build your GCC tool chain: here are some instructions. Once that is done you should be all ready to get started.

First things first, I am assuming you’ll be using avr-libc to make use of all the defines which it sets for you (register names etc), gcc, gdb and avrdude (at a minimum).

First things first I tend to define the device I am using at the top of the source file just in case. Then we need to include the AVR libc file which gives us the nice names for ports etc.

#ifndef __AVR_ATmega168__
#define __AVR_ATmega168__
#endif
 
#include <avr/io.h>

We include the AVR libc io.h header file so that we can use the libc defines to access the relevant ports by name rather than having to use their real address or having to define them ourselves.

Now we move on to the actual assembly program. At first I was stuck because the linker complained that no main method was declared. I found this somewhat perplexing until I remembered that you need to declare a main method using the .global statement. So don’t forget to have a section defined with a .global main.

     rjmp Init
 
Init:
 
     ser r16
     out _SFR_IO_ADDR(DDRB), r16
     out _SFR_IO_ADDR(DDRD), r16
 
     clr r16
 
     out _SFR_IO_ADDR(PORTB), r16
     out _SFR_IO_ADDR(PORTD), r16
 
     .global main
 
main:
 
     sbi _SFR_IO_ADDR(PORTB), 0
     rjmp main

The above code is simply meant to turn an LED on and was (very slightly) adapted from the “AVR: An Introductory Course” book.

It is vital that you save this file with an extension of .S (that is a capital S) as that is what tells GCC to run it through the assembler.

We will compile it with the following command:

avr-gcc ledon.S -mmcu=atmega168 -Os -g -o ledon.out

you will need to substitute the atmega168 part with the correct device that you are using. Failure to do so will result in a program compiled for the wrong chip.

We then need to create a hex file from the output which we can then upload to our device using AVRdude. Use this command:

avr-objcopy -O ihex ledon.out ledon.hex

You can find out how to upload it to the device here.

And there you have it. Debugging it seems somewhat more complex if you don’t have a JTAG (such as the Arduino, I’ll need to look into getting something like the AVR Dragon before I can use AVaRICE and GDB together).

Compiling and Installing NASM on Mac OS X

Posted in Programming on August 19th, 2009 by Simon Connah – 6 Comments

First things first, make sure you have installed the latest version of Xcode from the Apple Developer website (the version that comes on the Mac OS X DVDs is most likely an older version). Once that is installed you need to download the latest source code distribution of NASM from here, it is located under the Download tab.

Once you have the file downloaded (it should be called something like nasm-x-xx.tar.bz2 – if it has RC in the name download the next lowest number that does not have RC in the file name as we want to use a stable version for the purposes of learning) just double click on it to extract its contents into a folder.

Now open up the Terminal application which is located in /Applications/Utilities/ and type “cd” followed by a space. Then drag the folder that was just extracted from the nasm archive and drop it onto the Terminal window. This should place the path to the folder after the cd and space. If it looks something like:

cd ~/Downloads/nasm-2.07

then you have done it correctly. Press return and the terminal should place you inside the nasm distribution folder. Type the following commands exactly as they appear below:

./configure
 
make
 
sudo make install

after you have typed sudo make install it will ask you for your password. This is your administrator account password. It will not show anything as you type as a security measure so make sure you don’t forget how far you have got :) .

Now inside the Terminal window if you type

nasm -v

you should see the following (or something similar) output:

NASM version 2.07 compiled on Aug 13 2009

congratulations, you now have the latest version of NASM installed. If you have any questions please leave a comment and I will try my best to answer them (perhaps in a new blog post).