Speaker 1: Welcome to your deep dive. We're going to be looking
Speaker 1: at Raspberry Pie assembly language programming, specifically excerpts from the
Speaker 1: book Raspberry Pie Assembly Language Programming by Steven Smith. It's
Speaker 1: a twenty nineteen book, awesome, And you know you want
Speaker 1: to learn how to program your Raspberry Pie using assembly, right,
Speaker 1: and we're going to help you get the most out
Speaker 1: of this book.
Speaker 2: Yeah, this deep dive is your shortcut to understanding all
Speaker 2: the core concepts of assembly, Okay, and it'll give you
Speaker 2: the knowledge to write more efficient code nice potentially even
Speaker 2: create your own operating systems or programming languages.
Speaker 1: Okay, so let's get into it. Yeah, why even learn
Speaker 1: assembly in a world full of Python and JavaScript and
Speaker 1: C plus plus and all these high level languages. Don't
Speaker 1: they make assembly kind of Yeah, that's.
Speaker 2: A great question. Obsolete, it is, and the book actually
Speaker 2: addresses that right away.
Speaker 1: Okay.
Speaker 2: You know, while high level languages are great for lots
Speaker 2: of tasks, assembly is still relevant for a bunch of
Speaker 2: different reasons. So think about it this way. High level
Speaker 2: languages let you drive a car without even knowing how
Speaker 2: the engine works. Right. Assembly is like being a mechanic.
Speaker 2: You can tinker with the engine directly.
Speaker 1: Okay, so more control and power, but also more complexity exactly.
Speaker 2: So learning assembly gives you that deeper understanding of how
Speaker 2: the Raspberry Pies processor works. It's built on the ARM architecture, okay,
Speaker 2: and this knowledge is really useful for like writing highly
Speaker 2: optimized code, understanding computer architecture at a fundamental level, creating
Speaker 2: low level software like operating systems, or even like reverse
Speaker 2: engineering or security analysis.
Speaker 1: Yeah, creating an operating system with assembly sounds intense, but
Speaker 1: I guess that's that level of control we're talking about.
Speaker 2: Yeah, that's right.
Speaker 1: So to unlock the secrets of assembly, we need to
Speaker 1: understand this ARM architecture first.
Speaker 2: Right, absolutely, The airm architecture is like a blueprint for
Speaker 2: your Raspberry Pies processor, okay. And one of the key
Speaker 2: concepts here is that airm uses a load store architecture
Speaker 2: that before any data can be processed, it has to
Speaker 2: first be loaded from memory into registers. These registers are
Speaker 2: like the processors workbenches, interesting, so the data needs to
Speaker 2: be on the workbench before the processor can work with it.
Speaker 1: So there's this constant movement of data between memory and registers.
Speaker 1: That's right, isn't that inefficient.
Speaker 2: You might think so, but it's actually a very efficient system.
Speaker 1: Okay.
Speaker 2: The book also explains how the thirty two bit ARM
Speaker 2: architecture handles fitting both addresses and data, which are also
Speaker 2: thirty two bits long, into instructions okay, and then, of course,
Speaker 2: the program counter acts as a guide, always pointing to
Speaker 2: the next instruction and memory.
Speaker 1: Okay, I'm starting to get the picture, but you know me,
Speaker 1: I always learn best by seeing code and action. The
Speaker 1: book starts with the classic Hello World example, Right, yeah,
Speaker 1: what can we learn from that?
Speaker 2: The Hello World example is a great starting point.
Speaker 1: Okay.
Speaker 2: Even in this simple program we see fundamental concepts like
Speaker 2: the dot data section for storing data, the MOV instruction
Speaker 2: for moving data into registers, and the LDR instruction for
Speaker 2: loading data from memory.
Speaker 1: Okay, so, even with Hello World, we're already talking directly
Speaker 1: to the hardware.
Speaker 2: Got it?
Speaker 1: What else makes the assembly version of this program special?
Speaker 2: This example also introduces us to the idea of system calls.
Speaker 1: Okay.
Speaker 2: So to print Hello World to the console, the program
Speaker 2: has to interact with the Linux operating system. It does
Speaker 2: this through a system call using the SDC instruction. Okay,
Speaker 2: and there's even a tool called obdump that lets us
Speaker 2: disassemble the program, revealing the underlying binary code that the
Speaker 2: processor actually executes.
Speaker 1: Whoa, Okay, that's getting down to the nitty gritty right
Speaker 1: Before we move on, though, can we talk about numbers
Speaker 1: for a second. Sure, binary math has always intimated me
Speaker 1: a little bit. How does assembly handle that?
Speaker 2: You're not alone?
Speaker 1: Okay? Good.
Speaker 2: The book explains how computers use the system called two's
Speaker 2: compliment to represent negative numbers. It might seem strange at first,
Speaker 2: but it actually simplifies arithmetic operations for the processor.
Speaker 1: Okay.
Speaker 2: We also touch on the concept of indianness Danus. It's
Speaker 2: the way multibyte data is ordered in memory, which varies
Speaker 2: between computer architectures. The Raspberry Pi uses little endian format.
Speaker 1: Okay. So we've got this whole system for handling negative
Speaker 1: numbers and figuring out how data is arranged, right, What
Speaker 1: about actually doing calculations with assembly?
Speaker 2: So the book dives into instructions like mv and MVN
Speaker 2: for moving data and calculating one's compliment okay, which is
Speaker 2: related to two's complement.
Speaker 1: Okay.
Speaker 2: Then there are eightyds instructions for addition, where adds even
Speaker 2: takes into account a potential carryover from the previous operation.
Speaker 2: You also learn how to use shifting and rotating bits
Speaker 2: to efficiently multiply or divide numbers by powers of two.
Speaker 1: This is all fascinating, but I can imagine writing all
Speaker 1: this code by hand could get pretty tedious.
Speaker 2: Yeah, for sure.
Speaker 1: Are there any tools that can help us out?
Speaker 2: You bet?
Speaker 1: Okay.
Speaker 2: The book introduces the g and U Assembler okay, or AS,
Speaker 2: which transforms your human readable assembly code into the machine
Speaker 2: code that the Raspberry Pie understands. Right. Then there's the
Speaker 2: g and U Debugger or GDB okay, a powerful tool
Speaker 2: that lets you step through your code, examine variables, and
Speaker 2: hunt down bugs.
Speaker 1: So AS is like a translator in GDB is like
Speaker 1: a detective for your code.
Speaker 2: It's exactly.
Speaker 1: That sounds way better than counting bits by hand, Yeah,
Speaker 1: it is.
Speaker 2: What about managing larger projects with lots of code?
Speaker 1: For that, there's the Make utility okay. It's like a
Speaker 1: project manager for your code. Interesting, automating the build process
Speaker 1: and making sure everything's organized. Okay. The book even provides
Speaker 1: examples of make files, which are like recipes for building
Speaker 1: your assembly programs.
Speaker 2: This is all starting to make sense, You got the
Speaker 2: tools and the instructions right. But so far we've only
Speaker 2: talked about simple programs. How do we create more complex
Speaker 2: logic in assembly? How do we make decisions and create
Speaker 2: programs that can do different things based on certain conditions.
Speaker 1: So that's where program flow control comes in, and that's
Speaker 1: what we'll be diving into in the next part of
Speaker 1: our deep dive.
Speaker 2: All right, let's do it. Welcome back to our deep
Speaker 2: dive into Raspberry Pie assembly language programming. Last time, we explored,
Speaker 2: you know, the fundamentals of ARM architecture and some basic
Speaker 2: assembly instructions. We even learned about tools that make assembly
Speaker 2: programming easier, and we ended on the topic of program
Speaker 2: flow control, which is how we add decision making and
Speaker 2: more complex logic to our programs.
Speaker 1: Exactly, I'm really eager to learn how to make our
Speaker 1: assembly programs, you know, smarter and more responsive to different situations.
Speaker 2: Well, the key to understanding program flow control lies in
Speaker 2: a special register called the current program Status Register or CPSR.
Speaker 1: CPSR like a dashboard.
Speaker 2: For your code, displaying important status flags that reflect the
Speaker 2: outcome of the last instruction executed.
Speaker 1: A dashboard, so it tells us what's happening inside.
Speaker 2: The program precisely, The CPSR contains flags like the zero
Speaker 2: flag or Z, which is set if the result of
Speaker 2: an operation is zero, the negative flag or N if
Speaker 2: there was a negative gotcha, the carry flag or see
Speaker 2: if an addition results in a carry, and the overflow
Speaker 2: flag or V if an arithmetic operation overflows.
Speaker 1: So these flags are like signals that indicate the current
Speaker 1: state of the program exactly.
Speaker 2: And this is where it gets interesting. Okay, we use
Speaker 2: the CMP instruction to compare values based on the comparison
Speaker 2: these flags in the cps are are set. Then we
Speaker 2: can use conditional branch instructions like b eq, bn, BLT
Speaker 2: and others to change the flow of execution based on
Speaker 2: those flags.
Speaker 1: So cmp sets the stage and then the branch instructions
Speaker 1: decide which path the code should take. It's like having
Speaker 1: to choose your own adventure story for our program.
Speaker 2: That's a great analogy. Awesome, And as your programs become
Speaker 2: more complex, it's important to write structured, organized code to
Speaker 2: avoid like a tangled mess of branches.
Speaker 1: Right.
Speaker 2: This is where the concept of structured programming comes in,
Speaker 2: minimizing unnecessary jumps and keeping your code clean.
Speaker 1: Okay, structured programming for the win. Yeah, but what about
Speaker 1: when we want to break down our programs into smaller,
Speaker 1: reusable chunks. Can we do that in assembly?
Speaker 2: Absolutely, that's where functions come in. Functions are like many
Speaker 2: programs within your main program, allowing you to group instructions,
Speaker 2: reuse logic, and make your code more modular.
Speaker 1: So it's like building with legos. We can create these
Speaker 1: reusable modules that we can just put together to make
Speaker 1: bigger programs exactly.
Speaker 2: And to manage the flow of execution between functions, we
Speaker 2: use this structure called the stack.
Speaker 1: Okay, the stack.
Speaker 2: The stack is a dedicated area of memory that operates
Speaker 2: like a pile of plates.
Speaker 1: Okay.
Speaker 2: Use push to place data onto the stack and pop
Speaker 2: to take it off.
Speaker 1: So it's a last in, first out LIFO system.
Speaker 2: Precisely, this is crucial for function calls. Okay. When a
Speaker 2: function is called, it's parameters, local variables, and the address
Speaker 2: to return to. After the function finishes, we call that
Speaker 2: the return address are pushed onto the stack. The BL
Speaker 2: instruction that stands for a branch with link handles this
Speaker 2: function call process.
Speaker 1: So when the function is done and it just pops
Speaker 1: everything back off the stack and returns to where it
Speaker 1: left off exactly.
Speaker 2: The stack is like a temporary workspace for functions. The
Speaker 2: book even goes into detail about the concept of stack frames,
Speaker 2: which are like individual compartments on the stack for each
Speaker 2: function call, keeping everything organized and preventing data from getting
Speaker 2: mixed up.
Speaker 1: That's pretty clever. It's amazing how assembly can be so structured,
Speaker 1: even though it's such a low level language. It is,
Speaker 1: but so far we've been mostly focused on the internal
Speaker 1: workings of our programs. Can we use assembly to interact
Speaker 1: with the outside world.
Speaker 2: Absolutely, But the next part of the book dives into
Speaker 2: how to communicate with the Linux operating system from our
Speaker 2: assembly code, which opens up a world of possibilities.
Speaker 1: Welcome back to our deep dive into Raspberry Pie assembly
Speaker 1: language programming. We've talked about a lot, you know, ARM architecture,
Speaker 1: program flow control, functions, the stack. Right last time, you
Speaker 1: mentioned interacting with the Linux operating system from assembly. Yeah,
Speaker 1: that sounds like a big step it is.
Speaker 2: The ability to tap into the operating systems capabilities really
Speaker 2: opens up a whole new level of functionality for your
Speaker 2: assembly programs. Okay, the book explains how we can make
Speaker 2: use of Linux system calls directly from our assembly code.
Speaker 1: So what kind of things can we actually do with
Speaker 1: these system calls?
Speaker 2: Lots of things.
Speaker 1: Okay.
Speaker 2: System calls are like requests we make to the operating system.
Speaker 1: Okay.
Speaker 2: We can read and write files, mannix processes, work with
Speaker 2: the network, even control hardware connected with the Raspberry Pie.
Speaker 1: Controlling hardware with assembly sounds pretty powerful, it is. Can
Speaker 1: you give me an example.
Speaker 2: Sure. One chapter of the book focuses specifically on programming
Speaker 2: the Raspberry pies gpio pins. GPIO that sounds for general
Speaker 2: purpose input output.
Speaker 1: Okay.
Speaker 2: This lets you interface with external devices like LEDs, sensors, motors,
Speaker 2: and more.
Speaker 1: So we could write assembly code to make an LED blink.
Speaker 2: You got it.
Speaker 1: That's pretty cool. How do we actually control these gpio
Speaker 1: pins from assembly though?
Speaker 2: The book describes two methods. The first involves interacting with
Speaker 2: the Linux GPI io device driver by reading and writing
Speaker 2: special files in the cisclass GPO directory.
Speaker 1: Okay.
Speaker 2: The second method involves directly accessing the gpio controllers registers
Speaker 2: using memory mapped IO.
Speaker 1: Memory mapped IO what is that?
Speaker 2: It's a technique where specific memory addresses are mapped to
Speaker 2: physical hardware registers.
Speaker 1: Okay?
Speaker 2: By writing to these memory addresses, we can directly control
Speaker 2: the hardware.
Speaker 1: Okay.
Speaker 2: The book explains this concept in detail, including the role
Speaker 2: of virtual memory and how it allows us to access
Speaker 2: physical memory.
Speaker 1: It's amazing how we can get so close to the
Speaker 1: hardware with assembly. It is so we can control the
Speaker 1: operating system. We can even control hardware. Is there anything
Speaker 1: assembly can't do well?
Speaker 2: Assembly is fantastic for low level tasks where speed and
Speaker 2: efficiency are crucial, okay, But for more complex applications, higher
Speaker 2: level languages like C or Python are often more practical.
Speaker 1: Okay.
Speaker 2: However, the book shows us that assembly doesn't have to
Speaker 2: live in isolation. It can work alongside these higher level languages.
Speaker 1: So we can combine the power of assembly with the
Speaker 1: flexibility of languages like C and Python.
Speaker 2: Exactly. The book explains how to cool C functions from assembly,
Speaker 2: and how to create assembly libraries that can be used
Speaker 2: by C programs. Wow. You can even embed assembly code
Speaker 2: directly within C code using gccs ASM statement, And for
Speaker 2: Python you can use the c types module to call
Speaker 2: assembly functions.
Speaker 1: That opens up a lot of possibilities.
Speaker 2: It does, Okay.
Speaker 1: Before we wrap up, you mentioned earlier that the book
Speaker 1: covers advanced arithmetic operations multiplication and division. Can you tell
Speaker 1: me a little bit more about that?
Speaker 2: Sure? Multiplication and ERM assembly can involve instructions like mool,
Speaker 2: which gives you the lower thirty two bits of the results,
Speaker 2: or instructions like symbol and umll, which calculate the full
Speaker 2: sixty four bit product. Division is handled by instructions like
Speaker 2: sdiv and UDIV on modern ARM and processors. Okay, but
Speaker 2: earlier versions didn't have dedicated division instructions.
Speaker 1: So they had to come up with clever workarounds exactly.
Speaker 2: The book delves into the history of division algorithms on ARM,
Speaker 2: which is fascinating interesting. It also covers multiply accumulating instructions
Speaker 2: like MLA and smlal, which combine multiplication and addition into
Speaker 2: a single operation, making them incredibly efficient for certain calculations.
Speaker 1: Yeah, that sounds like it would be really useful for
Speaker 1: tasks like image processing or machine learning.
Speaker 2: Absolutely.
Speaker 1: Yeah.
Speaker 2: And speaking of powerful features, the book also introduces us
Speaker 2: to the neon coprocessor neon, a specialized unit designed for
Speaker 2: parallel processing of vectors and matrices. This coprocessor extends the
Speaker 2: capabilities of the floating point unit, providing instructions for vector arithmetic,
Speaker 2: data rearrangement, and more.
Speaker 1: Neon sounds like it could really boost performance for certain applications.
Speaker 2: It can awesome. And finally, the book touches on the
Speaker 2: transition from thirty two bit to sixty four bit ARM
Speaker 2: known as arch sixty four. Okay, this newer architecture brings
Speaker 2: an expanded register set, changes to the function calling conventions,
Speaker 2: and even the removed of conditional execution, relying instead on
Speaker 2: sophisticated branch prediction techniques.
Speaker 1: Wow, we've covered so much. This deep dive has taken
Speaker 1: us from the very basics of AIRM assembly to advance
Speaker 1: features like neon and sixty four bit architecture. Right, it's
Speaker 1: incredible how much depth there is to this seemingly simple language.
Speaker 2: Yeah, it really highlights how assembly, even in a world
Speaker 2: of high level languages, continues to be relevant. It gives
Speaker 2: you a level of control and understanding that's just not
Speaker 2: possible with higher level abstractions.
Speaker 1: I'm definitely feeling inspired to learn more. But before we go,
Speaker 1: there's one thing I'm still a bit apprehensive about. Yeah,
Speaker 1: reading and understanding existing assembly code. It seems so dense
Speaker 1: and cryptic.
Speaker 2: It can be challenging, but remember assembly code is built
Speaker 2: on the same fundamental concepts we've been discussing. The book
Speaker 2: provides some great strategies for tackling this challenge. Okay, the
Speaker 2: key is to approach it methodically.
Speaker 1: Methodically how so, first.
Speaker 2: Understand the context of the code, what's its purpose, what
Speaker 2: problem is it trying to solve. Then break the code
Speaker 2: down into smaller, more manageable chunks. Look for familiar patterns,
Speaker 2: data structures, arithmetic operations, control flow structures.
Speaker 1: So it's like solving a puzzle piece by piece exactly.
Speaker 2: And don't be afraid to use tools. A debugger is
Speaker 2: invaluable for stepping through code, examining values, and seeing how
Speaker 2: the instructions affect the program state. Okay, for really complex code,
Speaker 2: there are even tools like Guidra which can help decompile
Speaker 2: assembly code back into a higher level representation, making it
Speaker 2: easier to analyze.
Speaker 1: This Deep Dog has been an incredible journey. We've explored
Speaker 1: the power and flexibility of Raspberry Pie assembly language programming,
Speaker 1: going from basic concepts to advanced techniques it has.
Speaker 2: It's been amazing, and most importantly, we've seen that assembly
Speaker 2: is not some arcane forgotten language. It's a vital tool
Speaker 2: for understanding how computers work at the most fundamental level, yeah,
Speaker 2: giving you the ability to write highly efficient code and
Speaker 2: even push the boundaries of what's possible with hardware.
Speaker 1: A huge thanks to Steven Smith for writing Raspberry high
Speaker 1: assembly language programming, and to you our listeners for joining
Speaker 1: us on this deep dive. Until next time, keep exploring,
Speaker 1: keep learning, and keep pushing the limits of what you
Speaker 1: can create.