Uncategorized

ARM Assembly language

Asked 
Active today
Viewed 6 times
0

I have been struggling to light an LED using my raspberry pi 4 model b but have not been able to find a solution.

My assembly code :

.section .init
.globl _start
_start:

mov r0, #0xF0000000
orr r0, #0x0E000000
orr r0, #0x00200000
orr r0, #0x00000004
mov r1, #1
lsl r1, #18
str r1, [r0]

mov r0, #0xF0000000
orr r0, #0x0E000000
orr r0, #0x00200000
orr r0, #0x0000001C
mov r1, #1
lsl r1, #16
str r1, [r0]

loop$:
b loop$

I am trying to light up the LED through GPIO 16. A post on the raspberry pi forums suggests that the peripheral memory base has changed to 0xFE000000 instead of 0x20000000 + the GPIO offset; neither of those memory addresses worked to light the LED.

I also installed NOOBS on a different SD card and wrote a python script to light the LED and that worked.

To learn about the bare metals of raspberry pi, I have been reading the Baking Pi tutorial by the University of Cambridge.

Any help would be great.

 New contributor
  • I skimmed Baking Pi’s first two lessons and found this tutorial very good for newbies. I would rate it top 1% of the 100+ online tutorials I have read all these years. Lesson 1 actually shows every step to turn on GPIO 16 LED. Is there a step you could not follow or got stuck? – tlfong01 1 hour ago   
  • I skimmed the Troubleshooint section and read the following: Troubleshooting: cl.cam.ac.uk/projects/raspberrypi/tutorials/os/… “This course has not yet been updated to work with the Raspberry Pi models B+ and A+. Some elements may not work, in particular the first few lessons about the LED. It has also not been updated for Raspberry Pi v2” … In other words, the tutorials is perhaps 5 years out of date! – tlfong01 18 mins ago    
  • So you know how to use python to turn on a LED. But why now diving deep down to ARM assembly/machine language? – tlfong01 4 mins ago   Edit   

0

Question

How to use Rpi4B ARM assembly language to turn on a LED connected to GPIO 16?

/ to continue, …

Answer

Introduction

I studied digital computer fundamentals (Ref x) last century. I remember I learned things like voltage and current, Ohm’s Law, transistor, register, ALU, CPU etc. Most of the things I learned then are out of date. For example I learned Motorola 6800 and Intel 8086 CISC assembly language, but now everybody go RISC. So I need to relearn. In other words, the old dog is learning new tricks, …

Learning Plan

I think I will start with Cambridge U’s Baking Pi tutorial Lessons 0, 1, to learn how to turn on a LED, …

/ to continue, …

Learning Notes

/ to continue, …

References

(1) Baking Pi – Operating Systems Development – Computer Science and Technology Dept, Cambridge U

(2) Lesson 0 Introduction

(3) Lesson 1 OK01

(4) ARM Intructions Reference

(5) Troubleshooting

This course has not yet been updated to work with the Raspberry Pi models B+ and A+. Some elements may not work, in particular the first few lessons about the LED. It has also not been updated for Raspberry Pi v2.

/ to continue, …

Appendices

(A) Tutorial ARM Instructions

The following is a list of all the instruction boxes in the courses in order.

ldr reg,=val puts the number val into the register named reg.

mov reg,#val puts the number val into the register named reg.

lsl reg,#val shifts the binary representation of the number in reg by val places to the left.

str reg,[dest,#val] stores the number in reg at the address given by dest + val.

name: labels the next line name.

b label causes the next line to be executed to be label.

sub reg,#val subtracts the number val from the value in reg.

cmp reg,#val compares the value in reg with the number val.

Suffix ne causes the command to be executed only if the last comparison determined that the numbers were not equal.

.globl lbl makes the label lbl accessible from other files.

mov reg1,reg2 copies the value in reg2 into reg1.

Suffix ls causes the command to be executed only if the last comparison determined that the first number was less than or the same as the second. Unsigned.

Suffix hi causes the command to be executed only if the last comparison determined that the first number was higher than the second. Unsigned.

push {reg1,reg2,...} copies the registers in the list reg1,reg2,... onto the top of the stack. Only general purpose registers and lr can be pushed.

bl lbl sets lr to the address of the next instruction and then branches to the label lbl.

add reg,#val adds the number val to the contents of the register reg.

Argument shift reg,lsl #val shifts the binary representation of the number in reg left by val before using it in the operation before.

lsl reg,amt shifts the binary representation of the number in reg left by the number in amt.

str reg,[dst] is the same as str reg,[dst,#0].

pop {reg1,reg2,...} copies the values from the top of the stack into the register list reg1,reg2,.... Only general purpose registers and pc can be popped.

alias .req reg sets alias to mean the register reg.

.unreq alias removes the alias alias.

lsr dst,src,#val shifts the binary representation of the number in src right by val, but stores the result in dst.

and reg,#val computes the Boolean and function of the number in reg with val.

teq reg,#val checks if the number in reg is equal to val.

ldrd regLow,regHigh,[src,#val] loads 8 bytes from the address given by the number in src plus val into regLow and regHigh.

.align num ensures the address of the next line is a multiple of 2num.

.int val outputs the number val.

tst reg,#val computes and reg,#val and compares the result with 0.

mla dst,reg1,reg2,reg3 multiplies the values from reg1 and reg2, adds the value from reg3 and places the least significant 32 bits of the result in dst.

strh reg,[dest] stores the low half word number in reg at the address given by dest.

(B) …

/ to continue, …

.END

Categories: Uncategorized

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: