$30
CSC 230
1
Lab 2 Introduction to Assembly Language
Submit main.asm.
I. Introduction to Atmel Studio 7.0 1
Launch Atmel Studio 7.0 and create a new project named “lab2”: click on the Start button, then click
on Atmel Studio 7.0:
CSC 230
2
Create a new project named lab2: on the menu, click on File -> New -> Project:
In the new dialog box, on the left pane, under Installed, select Assembler. Type the project Name lab2 and
select the Location. Click on the OK button.
CSC 230 Fall 2019
3
Then a new dialog box appears, choose ATmega2560 for the Device Family. Click on the OK button.
The new project looks like this:
CSC 230 Fall 2019
4
Remove “start:
inc r16
rjmp start”
Type the following code:
CSC 230 Fall 2019
5
Save the code and build the program: on the menu, click on Build -> Build Solution:
CSC 230 Fall 2019
6
The screen looks like this:
If there are any errors, you must fix them and rebuild your program until there are no build errors.
Now, you are ready to run your program using the simulator. Set up the configuration of the simulator:
from the Project menu -> lab2 Properties…:
CSC 230 Fall 2019
7
In the settings window, click on Tool and select Simulator:
Save the project by click on “File” menu -> “Save All”.
Start the simulator: from the menu, click on Debug -> Start Debugging and Break
CSC 230 Fall 2019
8
The editor should show a yellow arrow indicating the instruction about to be fetched. The left panel
shows the “Processor Status”, where you can examine the register and processor values. The “Program
Counter” shows the memory address of the next instruction to be fetched.
Before executing any instructions, examine the program memory. It looks like this, verify the opcode:
CSC 230 Fall 2019
9
Fetch and execute the first instruction: click on the Step Into button (or press the F11 key)
Or go to the menu, click on Debug ->Step Into:
CSC 230 Fall 2019
10
After executing the first instruction, the value in register 16 (R16) is changed from 0x00 to 0x01. The
changed values are in red, so are the Program Counter, Cycle Counter, in addition to R16:
Stop the debugging session by clicking on the “Stop Debugging” command under the “Debug” menu.
II. Write some assembly language code (mnemonics) and convert it to machine instruction. Verify
the result using the machine code (hexadecimal numbers) in the memory. Are they big-endian or
little-endian? Choose one line of code and figure out its machine instruction.
CSC 230 Fall 2019
11
How to convert mnemonics to machine instruction?
On page 94 of the AVR Instruction Set Manual:
So the opcode (operation code) of ldi Rd, K is 1110 KKKK dddd KKKK
CSC 230 Fall 2019
12
For example:
Mnemonics: ldi r16, 0x82 ;load 0x82 to register 16
In this example, K = 0x82, in binary, it is 0b10000010
d = 16, in binary, it is 10000 (Is this “ldi r0, 4” correct? No, because 16 ≤ d ≤ 31. That
means only R16 to R31 can be used in the ldi instruction.)
The 16-bit Opcode of ldi is: 1110 KKKK dddd KKKK
Step 1: The opcode for ldi is: 1110????????????
Step 2, fill in the high nibble of K, which is 1000, we have 11101000????????
Step 3, fill in the low nibble of K, which is 0010, we have 11101000????0010
Step 4, fill in the last four bits of destination register (why? Because only 4 bits are reserved for registers.),
which is 0000, we have 1110100000000010
Step 5, convert the 16 bit machine code to hexadecimal: 0xE802
But in the memory, you see 0x02E8, why? Because it is little endian. The low byte is written before the
high byte.
In summary, the machine code for ldi r16, 0x82 is:
machine instruction in binary form: 0b1110100000000010
machine instruction in hexadecimal form: 0xE802
machine instruction in hexadecimal form in AVR memory (little endian): 0x02E8
Lab exercise:
Write machine code of the following mnemonics:
a. Andi r16, 0b00000001 (verify it by checking the values in memory) (page 20)
b. ldi r17, -5 ;hint, two’s complement of -5
Do you get 0x0170 for “a” and 0x1BEF for “b”?
III. Comment out the program you have written so far and write a new program. The program
calculates the number of students in csc999. There are two lab sections B01 and B02. The number of
students registered:
B01: 23
B02: 21
The maximum enrollment for the course is 60. If the course enrollment is bigger than 60, set register 0 to
1, 0 otherwise. Store the course enrollment in register 19. Assume the total number of students is fewer
than or equal to 255.
In high-level programming language, it can be done like this:
unsigned int x=23
unsigned int y=21
unsigned int sum=x+y
boolean over_enrollment=false
CSC 230 Fall 2019
13
if (sum>60)
over_enrollment=true
Submit main.asm.
1. Adapted from the lab notes written by Dr. Bill Bird for csc 230 in the summer of 2018.