NASM Assembly programming Tutorial 01

SolidusCode
3 Apr 201207:43

Summary

TLDRThis tutorial introduces viewers to writing a basic assembly program similar to those in C or C++. The script guides through creating a file with the .ASM extension, setting up a data and text section, and using a 'printf' function to display a message on the screen. It explains the importance of stack frame setup and teardown, and concludes with compiling the program using NASM and GCC. The tutorial is designed to be accessible to those familiar with C programming, aiming to transition into the world of assembly language.

Takeaways

  • 📝 The tutorial is about writing a functioning assembly program similar to those in C or C++.
  • 📁 The first step is to create a file with the .ASM extension for assembly language.
  • 📊 A typical assembly program includes a data section for initialized data and a text section for assembly code, which may also include uninitialized variables.
  • 🔧 The tutorial demonstrates how to set up basic templates for an assembly program, including sections for data and code.
  • 🗑️ The script suggests removing the section for uninitialized variables if they are not needed for the program.
  • 🖥️ The program aims to display a message on the screen using a C-style function called 'printf'.
  • 📑 Comments and null-terminated strings are used to document and define messages in assembly language.
  • 🔗 Labels in assembly are treated as memory locations, which is important for referencing in code.
  • 🔝 The 'global' directive is used to make the 'main' label accessible outside the program for external calls.
  • 🔄 The tutorial covers setting up and destroying a stack frame using 'push EBP' and 'mov EBP, ESP' instructions.
  • 📡 The 'external printf' directive tells the assembler that the printf function is available externally and not defined within the current source code.
  • 🔧 The process of compiling an assembly program is explained, including using NASM for assembly and GCC for the final program compilation.
  • 🎉 The tutorial concludes with the successful display of a message in assembly, indicating a working program.

Q & A

  • What is the purpose of the tutorial in the transcript?

    -The purpose of the tutorial is to guide the user through writing a functioning assembly program similar to those found in C or C++.

  • What file extension is suggested for an assembly program?

    -The suggested file extension for an assembly program is .ASM.

  • What are the two main sections of an assembly program as described in the transcript?

    -The two main sections of an assembly program are the data section for initialized data and the text section for the actual assembly code and uninitialized variables.

  • Why might one choose to not include an uninitialized data section in an assembly program?

    -One might choose not to include an uninitialized data section if the program does not require the use of uninitialized variables.

  • What is the purpose of the 'printf' function in the context of this assembly program?

    -The 'printf' function is used in the assembly program to display a message to the screen, similar to its use in C programming.

  • What does the 'global main' directive do in assembly language?

    -The 'global main' directive makes the 'main' label available outside of the immediate program, allowing other programs to find and start from it.

  • What is the purpose of the 'push EBP' instruction in setting up a stack frame?

    -The 'push EBP' instruction saves the base pointer of the caller and sets up a new stack frame by moving the stack pointer into EBP.

  • What does the 'external printf' declaration indicate in the assembly program?

    -The 'external printf' declaration tells the assembly program that the 'printf' function is available externally and not declared within the source code itself.

  • What is the process for compiling an assembly program as described in the transcript?

    -The process involves using an assembler like NASM to create an object file (.o), and then using a compiler like GCC to link the object file and produce the final executable program.

  • What command is used to assemble the .ASM file using NASM in the tutorial?

    -The command used to assemble the .ASM file using NASM is 'nasm -f elf32 asm1.asm -o asm1.o'.

  • How does the tutorial suggest testing the compiled assembly program?

    -The tutorial suggests testing the compiled assembly program by running it from the command line using './asm1' and observing if the expected message is displayed.

Outlines

00:00

📝 Introduction to Assembly Programming

This paragraph introduces the tutorial on writing an assembly program similar to those in C or C++. The speaker instructs viewers to create an ASM file and sets up the basic template for an assembly program, which includes a data section for initialized data and a text section for the actual assembly code. The text section also includes uninitialized variables, but these are omitted for the current program. The paragraph concludes with the setup for a simple program that will display a message on the screen using a C-style function called 'printf', which is familiar to those with C programming experience.

05:01

🖥️ Implementing printf in Assembly

The second paragraph delves into the process of implementing the 'printf' function in an assembly program. It explains how to set up a stack frame using the 'push EBP' instruction and creating a new base pointer. The speaker then details the steps to define the message to be printed and how to use 'printf' by declaring it as an external function, indicating it's available outside the current source code. The paragraph outlines the assembly code structure, including the setup and destruction of the stack frame, and the function body that pushes the message's memory address onto the stack before calling 'printf'. The tutorial concludes with instructions on how to compile the assembly code using NASM and GCC, resulting in a successfully displayed message in assembly, and teases further in-depth assembly topics for the next tutorial.

Mindmap

Keywords

💡Assembly

Assembly refers to a low-level programming language that is used to write programs in a form that is close to machine code. It is one of the core concepts in the video, as the tutorial is focused on writing an assembly program. The script mentions creating an '.asm' file and setting up basic templates for an assembly program, which are foundational steps in assembly language programming.

💡Data Section

In the context of the video, the data section is a part of the assembly program where initialized data is stored. It is a key concept because it is one of the fundamental sections of an assembly program, alongside the text section. The script explains that this section includes variables with initial values that the program will use.

💡Text Section

The text section is where the actual assembly code resides. It is crucial for understanding the structure of an assembly program because it contains the executable instructions. The video script mentions that this section will include all the assembly code for the program being written.

💡Uninitialized Variables

Uninitialized variables are those that are declared but do not have an initial value set. The script discusses the presence of such variables in an assembly program's data section, although it later clarifies that for the simple program being demonstrated, uninitialized variables are not needed.

💡C Type Function

A C type function, as mentioned in the script, refers to a function that is written in the C programming language but can be used in other languages, such as assembly. The video specifically mentions using 'printf', a standard C function for formatted output to the console, within an assembly program.

💡printf

Printf is a function used in C and other programming languages for printing formatted strings to the standard output stream. In the video, 'printf' is used as an example of how to call a C function from within an assembly program to display a message on the screen.

💡Stack Frame

A stack frame is a memory structure that stores temporary data during the execution of a function. The script explains setting up a stack frame by pushing the EBP register and creating a new base pointer, which is a fundamental concept in managing function calls and local variables in assembly programming.

💡Global

In the context of assembly language, 'global' is a directive that makes a label or symbol accessible outside of its module or program. The script uses 'global main' to indicate that the 'main' function can be called from other programs, which is essential for understanding program entry points.

💡External

The 'external' keyword in assembly language is used to declare that a symbol or label is defined outside of the current source file. In the script, 'external printf' is used to inform the assembler that the 'printf' function is available externally, not defined within the assembly code itself.

💡NASM

NASM stands for Netwide Assembler, which is an assembler and disassembler for the Intel x86 architecture. The script mentions using NASM to assemble the '.asm' file into an object file with the '.nasm' command, demonstrating the use of a specific tool in the assembly programming process.

💡GCC

GCC stands for the GNU Compiler Collection, which is a compiler system supporting various programming languages. In the video, GCC is used to compile the object file generated by NASM into an executable program, illustrating the final step in the assembly programming workflow.

Highlights

Introduction to writing a functioning assembly program similar to those in C or C++.

Creating an ASM file for the assembly program.

Setting up a basic template for an assembly program with data and text sections.

Explanation of initialized and uninitialized data variables in assembly.

Erasing the uninitialized variables section as it's not needed for this program.

Using the printf function from C in an assembly program.

Defining the message and its termination with a null character for assembly display.

Explanation of labels as memory locations in assembly language.

Starting to code within the text section and setting up the stack frame with push EBP.

Creating a new base pointer for the stack frame.

Restoring the stack pointer to destroy the stack frame after the function.

Using the 'global' keyword to make the 'main' label accessible outside the program.

Calling the printf function and pushing the memory address of the message onto the stack.

Declaring the printf function as external to the assembly program.

Compiling the assembly program using NASM and GCC.

Executing the compiled program to display the message in assembly.

Preview of more in-depth assembly topics in the next tutorial.

Transcripts

play00:00

sorry welcome to another assembly

play00:01

tutorial we're going to write an actual

play00:04

functioning program that is similar to

play00:07

one that you'd find in C or C++ okay so

play00:11

let's get to it

play00:12

if you just make it go ahead and make a

play00:14

file call it assembly one ASM or

play00:17

whatever else you want to name it so we

play00:19

have our file with the ASM extension for

play00:23

assembly and so let's set up our basic

play00:25

templates for all of our assembly

play00:28

program

play00:34

and usually this is what you have for

play00:38

any typical assembly program that you

play00:42

want to write you have your your data

play00:44

section which is going to include

play00:47

initialized data and you're going to

play00:52

have your text section which is going to

play00:54

include all your actual assembly code

play00:57

and here you're going to have your

play01:01

uninitialized variables so these are

play01:08

four variables and these are four

play01:10

variables as well and you can put these

play01:12

arrangement in in however way you please

play01:15

just so long as you have them and really

play01:20

if you don't use any uninitialized

play01:23

variables so you don't need to have on

play01:25

so let's go ahead and erase it for now

play01:27

because for this program we're not going

play01:29

to actually need one okay so now let's

play01:33

write a simple program that displays

play01:35

something to the screen but in this case

play01:39

we're not going to use the system call

play01:40

and we're not going to use the Linux

play01:44

system call we're actually going to use

play01:46

a real C type of function called the

play01:51

printf if you've done any C programming

play01:55

you're very well aware of what this is

play01:58

but it's just a a function for those of

play02:01

you who don't already know

play02:03

ok so let's actually define some things

play02:07

that we're going to be printing to the

play02:08

screen and first of all we're going to

play02:11

print out a message some message

play02:18

and zero so it's good to document stuff

play02:25

as you go ahead and write stuff um

play02:28

because here this means this is a

play02:30

comment and the zero is likes having a

play02:34

null at the end of your message so that

play02:37

when assembly displays this message it

play02:40

knows where to stop so this tells it to

play02:42

stop and ten is for our heritage return

play02:47

so we return to the next line and this

play02:50

is the actual message that we want to

play02:52

display okay so yeah that's it so and

play02:57

remember that our labels are treated as

play03:00

memory locations okay so now we're

play03:03

inside of our text section which is our

play03:06

code so we can actually begin coding

play03:08

what we're going to do now is do global

play03:12

main all this does it tells us that

play03:15

we're going to have a label called main

play03:18

and what global does it's a reserved

play03:21

word inside of the assembly language to

play03:25

have our symbol here main be able to be

play03:30

available outside of our immediate

play03:32

program so some other program later on

play03:35

the road will be able to find our

play03:38

program and know where to start

play03:40

okay so we have our label so let's get

play03:43

coding first thing we want to do is set

play03:46

up our stack frame which is push EBP

play03:49

because we're saving a whoever called

play03:52

main R which is in this case it's going

play03:54

to be an operating system that's going

play03:56

to call main they're going to save it's

play03:58

a base pointer and we're going to create

play04:02

a new one by doing

play04:06

okay so this takes the stack pointer and

play04:10

moves it into EBP based on to effective

play04:15

of creating a new one so that's step

play04:17

setting up our stack frame and so now

play04:20

let's destroy your stack frame after

play04:23

we're done with the function so I'm

play04:26

going to do

play04:29

we're going to restore this one that we

play04:32

say we're going to restore the stack

play04:34

pointer

play04:41

and so this is entire this is the entire

play04:44

function it's like having it's like

play04:47

having Lord main and in between the

play04:54

parentheses here is where our code will

play04:56

go so that's what's going on now let's

play05:00

go ahead and call this function or print

play05:03

out the message to the screen and how we

play05:05

do that is by saying push what this does

play05:08

is it remember this is our label up here

play05:11

it pushes this address so all this does

play05:14

is translate to something like push lets

play05:19

it as a memory address it pushes a

play05:21

memory address into onto the stack and

play05:24

now we're going to call printf I forgot

play05:29

to mention them before we call printf

play05:30

what we need to be able to make sure

play05:32

that our program knows we're knows that

play05:37

out we're using a variable that has not

play05:39

been declared in our program but is

play05:41

available somewhere else and how we do

play05:44

that is by saying external printf that's

play05:49

all this tells the assembly program that

play05:53

our printf function is not inside of our

play05:57

a source code it's available externally

play06:02

simple enough ok so what we have here is

play06:06

the creation of the stack destruction of

play06:09

the stack our function body and we push

play06:13

the memory address of our message and we

play06:15

calling the printf function and that's

play06:17

that's it so we save our file and we go

play06:20

to the command line and we're going to

play06:22

say you know this dollar file or file is

play06:25

there so we're going to say nessam which

play06:28

is net wide assembler file formats e.l.f

play06:33

output to a SM dot one that's the name

play06:37

of the father we're going to be creating

play06:39

and the input file is going to be a SM

play06:42

one a SM ok

play06:45

and Don's you can see it creates the

play06:48

file that we're looking for now what

play06:51

we're going to do now is use GCC the

play06:54

general C++ compiler and output the

play06:58

final program this is the output and use

play07:01

the input as a s m10 and it should

play07:06

compile successful and as you can see

play07:08

here this is our actual program and now

play07:11

we're going to do dots for slash asm1

play07:15

and if all goes well it should compile

play07:17

successfully and as we just saw it

play07:20

actually did display our message in

play07:22

assembly so all right we have just gone

play07:26

through a successful simple C problem

play07:29

sorry assembly program and written

play07:32

something that works in the next

play07:34

tutorial we'll going into more depth and

play07:36

show you some more cool stuff with

play07:38

assembly head and rate and subscribe

play07:40

okay thank you I'll see you next time

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
Assembly ProgrammingC Languageprintf FunctionStack FrameNASMGCCELF OutputTutorialProgramming BasicsCode Compilation
هل تحتاج إلى تلخيص باللغة الإنجليزية؟