NASM Assembly programming Tutorial 01
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
π 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.
π₯οΈ 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
π‘Data Section
π‘Text Section
π‘Uninitialized Variables
π‘C Type Function
π‘printf
π‘Stack Frame
π‘Global
π‘External
π‘NASM
π‘GCC
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
sorry welcome to another assembly
tutorial we're going to write an actual
functioning program that is similar to
one that you'd find in C or C++ okay so
let's get to it
if you just make it go ahead and make a
file call it assembly one ASM or
whatever else you want to name it so we
have our file with the ASM extension for
assembly and so let's set up our basic
templates for all of our assembly
program
and usually this is what you have for
any typical assembly program that you
want to write you have your your data
section which is going to include
initialized data and you're going to
have your text section which is going to
include all your actual assembly code
and here you're going to have your
uninitialized variables so these are
four variables and these are four
variables as well and you can put these
arrangement in in however way you please
just so long as you have them and really
if you don't use any uninitialized
variables so you don't need to have on
so let's go ahead and erase it for now
because for this program we're not going
to actually need one okay so now let's
write a simple program that displays
something to the screen but in this case
we're not going to use the system call
and we're not going to use the Linux
system call we're actually going to use
a real C type of function called the
printf if you've done any C programming
you're very well aware of what this is
but it's just a a function for those of
you who don't already know
ok so let's actually define some things
that we're going to be printing to the
screen and first of all we're going to
print out a message some message
and zero so it's good to document stuff
as you go ahead and write stuff um
because here this means this is a
comment and the zero is likes having a
null at the end of your message so that
when assembly displays this message it
knows where to stop so this tells it to
stop and ten is for our heritage return
so we return to the next line and this
is the actual message that we want to
display okay so yeah that's it so and
remember that our labels are treated as
memory locations okay so now we're
inside of our text section which is our
code so we can actually begin coding
what we're going to do now is do global
main all this does it tells us that
we're going to have a label called main
and what global does it's a reserved
word inside of the assembly language to
have our symbol here main be able to be
available outside of our immediate
program so some other program later on
the road will be able to find our
program and know where to start
okay so we have our label so let's get
coding first thing we want to do is set
up our stack frame which is push EBP
because we're saving a whoever called
main R which is in this case it's going
to be an operating system that's going
to call main they're going to save it's
a base pointer and we're going to create
a new one by doing
okay so this takes the stack pointer and
moves it into EBP based on to effective
of creating a new one so that's step
setting up our stack frame and so now
let's destroy your stack frame after
we're done with the function so I'm
going to do
we're going to restore this one that we
say we're going to restore the stack
pointer
and so this is entire this is the entire
function it's like having it's like
having Lord main and in between the
parentheses here is where our code will
go so that's what's going on now let's
go ahead and call this function or print
out the message to the screen and how we
do that is by saying push what this does
is it remember this is our label up here
it pushes this address so all this does
is translate to something like push lets
it as a memory address it pushes a
memory address into onto the stack and
now we're going to call printf I forgot
to mention them before we call printf
what we need to be able to make sure
that our program knows we're knows that
out we're using a variable that has not
been declared in our program but is
available somewhere else and how we do
that is by saying external printf that's
all this tells the assembly program that
our printf function is not inside of our
a source code it's available externally
simple enough ok so what we have here is
the creation of the stack destruction of
the stack our function body and we push
the memory address of our message and we
calling the printf function and that's
that's it so we save our file and we go
to the command line and we're going to
say you know this dollar file or file is
there so we're going to say nessam which
is net wide assembler file formats e.l.f
output to a SM dot one that's the name
of the father we're going to be creating
and the input file is going to be a SM
one a SM ok
and Don's you can see it creates the
file that we're looking for now what
we're going to do now is use GCC the
general C++ compiler and output the
final program this is the output and use
the input as a s m10 and it should
compile successful and as you can see
here this is our actual program and now
we're going to do dots for slash asm1
and if all goes well it should compile
successfully and as we just saw it
actually did display our message in
assembly so all right we have just gone
through a successful simple C problem
sorry assembly program and written
something that works in the next
tutorial we'll going into more depth and
show you some more cool stuff with
assembly head and rate and subscribe
okay thank you I'll see you next time
Browse More Related Video
How To Install C Programming Software In Laptop | C Installation Tutorial For Beginners |Simplilearn
How to Program A Calculator in C++! (Great C++ Microsoft Visual Studio Beginner Project)
1. Introduction to C Language in Tamil || Tamil Pro Techniques ||
C θͺθ¨ε ₯ι | 01 - 05 | ιθ¦ζΊεηε·₯ε ·
C++ Tutorial For Beginners | C++ Programming | C++ | C++ Basics | C++ For Beginners | Simplilearn
Beginner assignment 1 TIPS
5.0 / 5 (0 votes)