Assembly Programming 1 - Duke University

Compsci 104: Nios II Assembly Programming and the Nios II IDE

Running a Program under the NIOS II Instruction Set Simulator

1. Start the Nios II IDE. (Start->All Programs->Altera->NiosII EDS 6.1-> NiosII 6.1 IDE

2. Set the workspace to C:\CPS104\workspace create a new folder with the name workspace if necessary. (No spaces in any folder names).

3. Creat a new project. File->New->Project Nios II C/C++ Application 4. Select blankproject from templates 5. Check Specify location (for workspace) if needed. Should be

C:\CPS104\workspace. 6. Change the name of the project to asm1. 7. Set the SOPC Builder System. Browse to

C:\altera\61\nios2eds\examples\vhdl\niosII_cycloneII_2C35\full_featured\full_2C35.ptf (Note this may be available on the drop down menu if it was previously specified)

8. Click Next 9. Click specify system library 10. Check the box in front of the existing library that contains `full_2C35' (This is the

one we generated previously) 11. Click Finish 12. Add a file with name basics.s (File->New->Other

o This opens a popup where you can pick File from under the General Category, after selecting File, you need to specify the name at the bottom (use basics.s)

13. Enter the following Code:

# comments are started with the # sign, everything after is ignored

.text

#a directive, begin the text segment

.global main #a directive, declare main as a global variable

main:

# a label, it provides a name for a memory location

movi r8, 1

# set r8 to 1

addi r8, r8, 1 # increment r8 by 1

movia r9, foo # load address of foo into r9 ldw r10, 0(r9) # load value that is stored at foo into r10 addi r10, r10, 4 # increment value by 4 stw r10, 0(r9) # store the value back to foo

ret

# return from main

.data # a directive, begin the data segment

foo: # a label, a name for this part of memory

.word 0xa

#this is a directive to initialize memory

bar: # another label

.skip 256

# simply skip 256 bytes (uninitialized memory)

str: # another label

.asciz "This is a string"

# a blank line must be at the end so the assembler

doesn't complain...after this comment line...sigh

14. Now, use Debug As -> Nios II Instruction Set Simulator which will build the project (assemble the file) and start the debugger. Be patient...

15. In the middle right window slick on the Disassembly tab.

16. Click on the i symbol to start instruction stepping mode. (NOTE: It is

important that you do this to step through assembly programs.) 17. Click the Registers tab in the upper right window 18. Click on + before main to list the registers (note the commonly used hex sequence

0xdeadbeef in many of the registers) 19. Click step into observe r8 change 20. Repeat step into, observe r8 change 21. Repeat step into, observe r9 (Notice in the disassembly window that the movia is

actually two instructions.) 22. Repeat step into, observe r9 23. Set a memory monitor for the address that is now in r9 24. Step into, observe r10 change to the value at the location in memory 25. Step into, r10 increases by 4 26. Step into, observe the memory location change 27. Add new rendering of type ascii for the same address. 28. Scroll down until you see the string "This is a string".

Modify the above program to convert the string stored at str to all upper case. Lower case a is 97 and uppercase A is 65...

Input/Output

Create a new blank project and add a new file called inout.s Add the following code:

.text

.global main

main:

# start function

mov r16, r31

# save a copy of the return address

movia r4, input_fmt # put address of input_format in r4 (arg1)

call printf

# invoke printf function to display string

movia r4, scanf_str # put address of scanf format in r4

movia r5, string

# put address of string storage in r5 (arg2)

call scanf

# invoke scanf

movia r4, output_fmt # put address of out format string into r4 (arg1)

movia r5, string

# put string address into r5 (art2)

call printf

# invoke printf to display string

mov r31, r16

# put the copy of r31 back

ret

# return to calling function

.data input_fmt: .asciz "Please enter a string\n" scanf_str: .asciz "%s" string: .skip 256 output_fmt: .asciz "String is %s\n"

1. This is our first exposure to parameter (argument) passing into functions. As part of the Application Binary Interface (ABI), the first few parameters are passed through registers (r4 through r7 for the NiosII). (We will learn more about this next week in lecture). Recall that in C the call to printf has the form printf("format string", arg1, arg2,...,argn); and that strings are arrays of characters (a sequence of bytes stored in contiguous memory locations). Therefore, the parameters to printf are 1. the starting address of the format string, 2. the variables.

2. Run the program (as NiosII instruction set simulator). 3. After being prompted, type "Thisstringistoolong!" (without the quotes) 4. Why do we need the second instruction that sets r5 to the address of string? We

just did it... 5. Now change the .skip directive from 256 to 4 6. Run the program again....isn't java much better. 7. Debugging...debug as ISS, use "step over" to advance execution (remember to

click the instruction stepping mode). If you use step into you will end up in the printf/scanf function....if this happens use the step return (right most)

Caller saved registers: r8 through r15 Callee saved registers: r16 through r23 (use these for now when possible, more next week...)

Do not use r1 (it is assembler temp), there are other registers in 24-30 that you shouldn't use either...

File Transfer

Copy Files from/to your current laptop to the OIT machines using Secure FTP. If you don't know how, we'll go through it... You will need to do this if you use the Kiosk machines.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download