SlideShare uma empresa Scribd logo
1 de 53
By : BELLAJ BADR
Fouder of Raspberry pi Moroccan community
*C in Raspberry Pi
*How a C program is executed
*Hellow world in assembly
*Build your own OS « Hello world OS »
*
*
** in 1971-73 Dennis M. Ritchie turned
the B language into the C language,
keeping most of the language B syntax
while adding data-types and many
other changes
*
the most famous example program from the book is its "hello,
world" program, which just prints out the text "hello, world" to
the terminal, as an illustration of a minimal working C
program. Numerous texts since then have followed that
convention for introducing a programming language.
*in 1978 the publication of ”The C Programming
Language” by Kernighan & Ritchie caused a revolution
in the computing world.
*
#include <stdio.h>
int main(void){
printf("My first C programn");
return 0;
}
*
*Compiled language
*Interpreted language
*P-code language
*
*gcc hello.c
=>your code has been compiled into a separate
executable file which by default is named
as a.out
*To execute program first, enter: ./first
*
*The compilation is performed in four sequential phases
by the compilation system (a collection of four
programs preprocessor,compiler, assembler, and
linker).
* we could use the commands as (assembler), ld (link
loader), and gdb (GNU debugger) from GCC(GNU
Compiler Collection) .
*C code=> assembly code=>object file=>executable
*
*To get the assembly code from our C code we
use the « Gcc –s » command :
1
2
3
# gcc -S hello.c -o hello.s
# cat hello.s
*
1
2
3
# gcc -c hello.s -o hello.o
# file hello.o
hello.o: ELF 32-bit ,,,,,,,,,,,,
1
2
8 # readelf -a hello.o
We can see that the hello.o is the object file that is actually an ELF 32-bit
executable, which is not linked yet. If we want to run the executable, it will
fail as noted below:
We can read the contents of the object file with the readelf program
as follows
*
*# chmod +x hello.o
*# ./hello.o
*bash: ./hello.o: cannot execute binary file =>
we need to link it
*From object to excutable?
*
*Generic ELF File Layout : A simple Executable
ARM ELF file has the conceptual layout shown
in the diagram on the right.
*
we want to build object code for the ARM processor at the heart of the
Raspberry Pi, we need a cross-compiler and its associated tools, which
is usually called a "toolchain". Here we are using "crosstool-ng" to build
such tool chain.
arm - non e - eabi - gcc
*To understand the C compilation let’s look at
the ARM assembly language
*
In this lab, you will learn to write ARM assembly language
programs and test them on a Raspberry Pi, featuring the BCM2835
microprocessor
Rpi < 2 only support armv6 instructions
While RPI 2 support armv7 instructions
*all ARM instructions are 32 bits long. Here is a
typical one:
*10101011100101010010100111101011
*Fortunately, we don't have to write ARM
programs using such codes. Instead we use
assembly language
http://goo.gl/EVrLl0
Registre CPSR
*
Registres particuliers
r13 alias sp : stack pointer pointeur sur pile de
donnees
● r14 alias lr : lr stands for link register and it
is the address of the instruction following the
instruction that called us(return)
● r15 alias pc : program counter contains the
address of the next instruction going to be
executed
When the ARM processor
executes an instruction,
two things may happen at
the end of its execution. If
the instruction does not
modify pc (and most
instructions do not), pc is
just incremented by 4 (like
if we did add pc, pc, #4).
Why 4? Because in
ARM, instructions are 32
bit wide, so there are 4
bytes between every
instruction. If the
instruction modifies pc
then the new value for pc
is used
Once the processor has fully executed an instruction then it
uses the value in the pc as the address for the next instruction
to execute,This process of changing the value of pc is called
branching. In ARM this done using branch instructions.
cpsr (for Current Program Status
Register) keeps some values that can be read and updated
when executing an instruction
cmp r1, r2 /* updates cpsr doing "r1 ‐ r2", but r1 and r2
are not modified */
EQ (equal) When Z is enabled (Z is 1)
NEQ (not equal). When Z is disabled. (Z is 0)
[r1, +#12] => an offset of 12
(4 bytes * 3 items
skipped).
*
*Exemple d'instruction : addition
ADD r0,r1,r2 @ r0<-r1+r2
ADD r0,r0,#4 @ r0<-r0+4
SUB,
*L'instruction de ''mouvement'' de donnee
MOV rd,<Oprnd2> @ rd <- Oprnd2
*load : Memory =>registre
*store : registre=>memory
*.INCLUDE "fichier.s"
*Le format des instructions ARM est fixe :
*Une instruction fait 4 octets (32 bits)
*Exemple de codage d'une instruction :
*MOV r5,#7 @ Instruction Assembleur
*0b11100011101000000101000000000111
*=0xE3A05007 ( code langage machine)
*
*MOV r5,#7
*● Execution inconditionnelles : Always
->1110
*● Puis specification de famille d'operation
*( Data-processing : MOV, ADD, CMP ...)
->111000
*● Puis indicateur d'operande immediat (I)
->1110001
*
MOV r5,#7
*● C'est une operation Move =>Opcode
*->11100011101
*● Puis indicateur ''pas de mise a jour
*des codes conditions'' (pas de S en suffixe)
*->111000111010
*● MOV : pas de 1er operande (Rn a 0)
*->1110001110100000
*● Registre destination R5 (Rd code pour 5)
*->11100011101000000101
*Format immediat : constante 8 bits et rotation
*constante = 0b00000111(binaire)=7décimal
*● Pas besoin de deplacer la constante 8 bits
*pour obtenir la valeur immediate (rotation 0)
*->111000111010000001010000
*● Constante 8 bits
*->11100011101000000101000000000111
*
*MOV r5,#7
*0b1110001110100000010
1000000000111
*=0xE3A05007
.data
msg:
.ascii "Hello, Piday!n"
len = . ‐ msg
.text
.globl _start
_start:
/* syscall write(int fd, const void *buf, size_t count) needs 3 argument*/
mov %r0, $1 /* fd ‐> stdout standard output*/
ldr %r1, =msg /* buf ‐> msg */
ldr %r2, =len /* count ‐> len(msg) */
mov %r7, $4 /* write is syscall #4 */
swi $0 /* invoke syscall */
/* syscall exit(int status) */
mov %r0, $0 /* status ‐> 0 */
mov %r7, $1 /* exit is syscall #1 */
swi $0 /* invoke syscall */
The C function s, including the ISO C
standard ones, are widely used by
programs, and are regarded as if they
were not only an implementation of
something in the C language, but also
de fact o part of the operating system
interface.=> glibc
Making call using C lib
it is rather unusual to perform system calls directly.
It is almost always preferable to call the C library
instead.
*
*To terminate a program
*MOV R7, #1
*SVC 0 6 of 23
*The number 1 placed in Register 7 tells the operating system
to terminate this program. The instruction “SVC 0” is the
system call, that transfers the program execution to the
operating system. If you place a different number in R7, the
operating system will perform a difference service.
on ARM, the system call identifier is put in register R7, arguments are passed in
R0R6 (respecting “EABI arrangement” where appropriate,i.e. 64bit arguments),
and the kernel is called with the ‘SWI 0’ instruction.
Now, coming to Raspberry Pi, which is a Broadcom SOC,BCM 2835,based on ARM
Processor. Every System Call is Index in the System Call Table. The Index is an
Integer value which is passed to the Register R7, in case Platform. The
registers, R0, R1 and R2 are used to pass the arguments of the System Call. The
instruction, SWI, now being used as SVC, which is a Supervisor Call, used to
jump to the Privileged Mode, to invoke the Kernel. The embedded with SVC
#num, is used to refer to the Handler.
svc #0
Hence, as an example, say, we want to invoke a System Call to print "Hello
Worldn". The System Call Index 'Write' is #4. Thus, the code will be something
like,
*
In Linux ARM we can perform a system call by using the
instruction swi. This instruction means software
interruption and its sole purpose is to make a system
call to the operating system.
Linux we will always use swi #0 to perform a system call.
No system call in Linux receives
more than 7 arguments and the arguments
are passed in registers r0 to r6. If the
system call returns some value it will be
returned in register r0.
Hello world, the system call way
As a simple illustration of calling the operating system we
will write the archetypical “Hello world” program using
system calls. In this case we will call the function write.
Write receives three parameters: a file descriptor where we
will write some data, a pointer to the data that will be
written and the size of such data. Of these three, the most
obscure may be now the file descriptor. Without entering
into much details, it is just a number that identifies a file
assigned to the process. Processes usually start with three
preassigned files: the standard input, with the number 0,
the standard output, with the number 1, and the standard
error, with the number 2. We will write our messages to the
standard output, so we will use the file descriptor 1.
.arch armv6
.section .rodata
.align 2
.data
HelloWorldString:
.ascii "Hello Worldn"
.LC0:
.text
.align 2
.global main
.type main, %function
main:
mov r7, #4
mov r0, #1
ldr r1,=HelloWorldString
mov r2, #12
svc #0
@ Need to exit the program
mov r7, #1
mov r0, #0
svc #0
.L3:
.align 3
.L2:
.size main, .main
.ident "GCC: (Debian 4.6.314+
rpi1) 4.6.3"
.section .note.GNUstack,"",%
progbits
Compile and run , ./program
Now, check 'dmesg' using,
#dmesg | tail
Write assembly in .s file
As ‐o hello.o hello.S (output object file from
assembly file)
Ld ‐s ‐o hello hello.o (output exeutable file) gcc ‐o hello hello.o
*Linker: Finally, the linker ﴾ld/ld.exe﴿ links the object code with the
library code to produce an executable file « hello/hello.exe".
*> ld ‐o hello.exe hello.o ...libraries...
*
"ldd" Utility ‐ List Dynamic‐Link Libraries
The utility "ldd" examines an executable and displays a list of the
shared libraries that it needs. For example,
> ldd hello.exe
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll
(0x77600000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll
(0x75fa0000)
Gdb Debugger
>> gdb hello
>> gdb start
>> gdb disassemble
*Debugging :
* as gstabs o filename.o filename.s =>get assambly
* If you want to use gdb, you need to invoke the
assembler with some additional options.
* When gdb starts, we need to set a breakpoint.
* The execution of the program will stop there and
we can step forwards one instruction at a time
from
* that point. Here, I am setting the breakpoint
* at the _start label.
* (gdb) break *_start
* (gdb) run
* (gdb) info rgisters
* GDB has the ability of disassembling the machine
code back to assembly instructions. The command
is “disassemble”.
Gdb layout asm
Gdb si (stepinto)
* To start the program, use command “run”*
*
*Install Code::Blocks IDE
*To install Code::Blocks IDE, use the following
command at the command prompt and all the
required software will be installed.
*$ sudo apt-get install codeblocks
*The assembly code generated from the C code
is different from the basic Assembly code.s
A compiler has to produce working machine code for
the infinite number of programs that can be
written in the language it compiles. It is impossible
to ensure that all possible highlevel Instructions are
translated in the optimum way;
Call assembly into C
*
*
How a Raspberr y-Pi processor boots. The BCM2385 includes a GPU
and this GPU includes a
bootloader . The bootloader is capabable of reading the contents of
a FAT32 partition on an SD card and booting fr om
the kernel .img file contained on it. This kernel.img file is an ARM
executable, and is generally the linux kernel . All we need to do is
generate our executable and replace the kernel .img file on the SD
card with our file to execute it.
The first thing we will need to setup is the GPIO controller.
There are no drivers we can rely on as there is no OS
running, all the bootloader has done is boot the processor
into a working state, ready to start loading the OS.
*Now that you have extracted the template, create a new file
in the 'source' directory called 'main.s'. This file will contain
the code for this operating system. To be explicit, the folder
structure should look like:
*build/ (empty)
* source/
main.s
*kernel.ld
*LICENSE
*Makefile
Open 'main.s' in a text editor so that we
can begin typing assembly code. The
Raspberry Pi uses a variety of assembly
code called ARMv6, so that is what we'll
need to write in.
Copy in these first commands.
.section .init
.globl _start
_start:
*
*This will turn on
the LED and blink
To install your operating system, first of all get a Raspberry PI SD
card which has an operating system installed already. If you browse
the files in the SD card, you should see one called kernel.img.
Rename this file to something else, such as kernel_linux.img. Then,
copy the file kernel.img that make generated onto the SD Card.
You've just replaced the existing operating system with your own. To
switch back, simply delete your kernel.img file, and rename the
other one back to kernel.img. I find it is always helpful to keep a
backup of you original Raspberry Pi operating system, in case you
need it again.
BareMetal OS, a 64-bit operating system written entirely in
assembly.
APOTHEMEOS A Small Assembly Opensource Os.
*
*Cambridge tutorials by lighting the OK LED
on the Raspberry-Pi board.
*Any request : Bellaj1@gmail.com
*

Mais conteúdo relacionado

Mais procurados

Part III: Assembly Language
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly Language
Ahmed M. Abed
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
Mohd Arif
 

Mais procurados (20)

Examinable Question and answer system programming
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programming
 
Lecture6
Lecture6Lecture6
Lecture6
 
Part III: Assembly Language
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly Language
 
MASM -UNIT-III
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-III
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Assembly 8086
Assembly 8086Assembly 8086
Assembly 8086
 
Chapter 2 programming concepts - I
Chapter 2  programming concepts - IChapter 2  programming concepts - I
Chapter 2 programming concepts - I
 
Introduction to debugging linux applications
Introduction to debugging linux applicationsIntroduction to debugging linux applications
Introduction to debugging linux applications
 
Assembler
AssemblerAssembler
Assembler
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
 
Programming ATmega microcontroller using Embedded C
Programming ATmega microcontroller using Embedded CProgramming ATmega microcontroller using Embedded C
Programming ATmega microcontroller using Embedded C
 
06 - ELF format, knowing your friend
06 - ELF format, knowing your friend06 - ELF format, knowing your friend
06 - ELF format, knowing your friend
 
Introduction to Assembly Language
Introduction to Assembly Language Introduction to Assembly Language
Introduction to Assembly Language
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
It322 intro 3
It322 intro 3It322 intro 3
It322 intro 3
 
8086 assembly
8086 assembly8086 assembly
8086 assembly
 
Binary art - Byte-ing the PE that fails you (extended offline version)
Binary art - Byte-ing the PE that fails you (extended offline version)Binary art - Byte-ing the PE that fails you (extended offline version)
Binary art - Byte-ing the PE that fails you (extended offline version)
 
Assembly language part I
Assembly language part IAssembly language part I
Assembly language part I
 
POWER processor and features presentation
POWER processor and features presentationPOWER processor and features presentation
POWER processor and features presentation
 

Semelhante a C from hello world to 010101

Computer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docxComputer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docx
brownliecarmella
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
Quản Minh Tú
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
Ikhwan_Fakrudin
 

Semelhante a C from hello world to 010101 (20)

Computer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docxComputer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docx
 
Introduction to computer architecture .pptx
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptx
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
C tutorials
C tutorialsC tutorials
C tutorials
 
ISA.pptx
ISA.pptxISA.pptx
ISA.pptx
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
NASM Introduction.pptx
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptx
 
Assembler Programming
Assembler ProgrammingAssembler Programming
Assembler Programming
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
Writing c code for the 8051
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
 
07 processor basics
07 processor basics07 processor basics
07 processor basics
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
Computer Architecture Assignment Help
Computer Architecture Assignment HelpComputer Architecture Assignment Help
Computer Architecture Assignment Help
 
C Under Linux
C Under LinuxC Under Linux
C Under Linux
 
Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
 
Lect05 Prog Model
Lect05 Prog ModelLect05 Prog Model
Lect05 Prog Model
 
20 -miscellaneous
20  -miscellaneous20  -miscellaneous
20 -miscellaneous
 

Mais de Bellaj Badr

Mais de Bellaj Badr (14)

Cours4.pptx
Cours4.pptxCours4.pptx
Cours4.pptx
 
0240-formation-ssh-secure-shell.pdf
0240-formation-ssh-secure-shell.pdf0240-formation-ssh-secure-shell.pdf
0240-formation-ssh-secure-shell.pdf
 
5-Authentification.2P.pdf
5-Authentification.2P.pdf5-Authentification.2P.pdf
5-Authentification.2P.pdf
 
Is web 3 an overengineered solution
Is web 3 an overengineered solutionIs web 3 an overengineered solution
Is web 3 an overengineered solution
 
create your own cryptocurrency
create your own cryptocurrencycreate your own cryptocurrency
create your own cryptocurrency
 
Blockchain and bitcoin in numbers
Blockchain and bitcoin in numbersBlockchain and bitcoin in numbers
Blockchain and bitcoin in numbers
 
Blockchain Tokenization
Blockchain TokenizationBlockchain Tokenization
Blockchain Tokenization
 
Security in the blockchain
Security in the blockchainSecurity in the blockchain
Security in the blockchain
 
Blockchain demystification
Blockchain demystificationBlockchain demystification
Blockchain demystification
 
Bitcoin
BitcoinBitcoin
Bitcoin
 
An introduction to AI (artificial intelligence)
An introduction to AI (artificial intelligence)An introduction to AI (artificial intelligence)
An introduction to AI (artificial intelligence)
 
Connected Car Platform (CC-p)
Connected Car Platform (CC-p) Connected Car Platform (CC-p)
Connected Car Platform (CC-p)
 
the age of cryptocurrency at Devoxx Morocco
the age of cryptocurrency at Devoxx  Moroccothe age of cryptocurrency at Devoxx  Morocco
the age of cryptocurrency at Devoxx Morocco
 
beware of Thing Bot
beware of Thing Botbeware of Thing Bot
beware of Thing Bot
 

Último

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

C from hello world to 010101

  • 1. By : BELLAJ BADR Fouder of Raspberry pi Moroccan community
  • 2. *C in Raspberry Pi *How a C program is executed *Hellow world in assembly *Build your own OS « Hello world OS » *
  • 3. *
  • 4. ** in 1971-73 Dennis M. Ritchie turned the B language into the C language, keeping most of the language B syntax while adding data-types and many other changes
  • 5. * the most famous example program from the book is its "hello, world" program, which just prints out the text "hello, world" to the terminal, as an illustration of a minimal working C program. Numerous texts since then have followed that convention for introducing a programming language. *in 1978 the publication of ”The C Programming Language” by Kernighan & Ritchie caused a revolution in the computing world.
  • 6. * #include <stdio.h> int main(void){ printf("My first C programn"); return 0; }
  • 8. * *gcc hello.c =>your code has been compiled into a separate executable file which by default is named as a.out *To execute program first, enter: ./first
  • 9. * *The compilation is performed in four sequential phases by the compilation system (a collection of four programs preprocessor,compiler, assembler, and linker). * we could use the commands as (assembler), ld (link loader), and gdb (GNU debugger) from GCC(GNU Compiler Collection) . *C code=> assembly code=>object file=>executable
  • 10. *
  • 11.
  • 12.
  • 13. *To get the assembly code from our C code we use the « Gcc –s » command : 1 2 3 # gcc -S hello.c -o hello.s # cat hello.s
  • 14.
  • 15. * 1 2 3 # gcc -c hello.s -o hello.o # file hello.o hello.o: ELF 32-bit ,,,,,,,,,,,, 1 2 8 # readelf -a hello.o We can see that the hello.o is the object file that is actually an ELF 32-bit executable, which is not linked yet. If we want to run the executable, it will fail as noted below: We can read the contents of the object file with the readelf program as follows
  • 16. * *# chmod +x hello.o *# ./hello.o *bash: ./hello.o: cannot execute binary file => we need to link it
  • 17. *From object to excutable?
  • 18. * *Generic ELF File Layout : A simple Executable ARM ELF file has the conceptual layout shown in the diagram on the right.
  • 19. * we want to build object code for the ARM processor at the heart of the Raspberry Pi, we need a cross-compiler and its associated tools, which is usually called a "toolchain". Here we are using "crosstool-ng" to build such tool chain. arm - non e - eabi - gcc
  • 20. *To understand the C compilation let’s look at the ARM assembly language
  • 21. * In this lab, you will learn to write ARM assembly language programs and test them on a Raspberry Pi, featuring the BCM2835 microprocessor Rpi < 2 only support armv6 instructions While RPI 2 support armv7 instructions
  • 22. *all ARM instructions are 32 bits long. Here is a typical one: *10101011100101010010100111101011 *Fortunately, we don't have to write ARM programs using such codes. Instead we use assembly language
  • 25. * Registres particuliers r13 alias sp : stack pointer pointeur sur pile de donnees ● r14 alias lr : lr stands for link register and it is the address of the instruction following the instruction that called us(return) ● r15 alias pc : program counter contains the address of the next instruction going to be executed When the ARM processor executes an instruction, two things may happen at the end of its execution. If the instruction does not modify pc (and most instructions do not), pc is just incremented by 4 (like if we did add pc, pc, #4). Why 4? Because in ARM, instructions are 32 bit wide, so there are 4 bytes between every instruction. If the instruction modifies pc then the new value for pc is used Once the processor has fully executed an instruction then it uses the value in the pc as the address for the next instruction to execute,This process of changing the value of pc is called branching. In ARM this done using branch instructions.
  • 26. cpsr (for Current Program Status Register) keeps some values that can be read and updated when executing an instruction cmp r1, r2 /* updates cpsr doing "r1 ‐ r2", but r1 and r2 are not modified */ EQ (equal) When Z is enabled (Z is 1) NEQ (not equal). When Z is disabled. (Z is 0) [r1, +#12] => an offset of 12 (4 bytes * 3 items skipped).
  • 27. * *Exemple d'instruction : addition ADD r0,r1,r2 @ r0<-r1+r2 ADD r0,r0,#4 @ r0<-r0+4 SUB, *L'instruction de ''mouvement'' de donnee MOV rd,<Oprnd2> @ rd <- Oprnd2 *load : Memory =>registre *store : registre=>memory *.INCLUDE "fichier.s"
  • 28. *Le format des instructions ARM est fixe : *Une instruction fait 4 octets (32 bits)
  • 29. *Exemple de codage d'une instruction : *MOV r5,#7 @ Instruction Assembleur *0b11100011101000000101000000000111 *=0xE3A05007 ( code langage machine)
  • 30.
  • 31. * *MOV r5,#7 *● Execution inconditionnelles : Always ->1110 *● Puis specification de famille d'operation *( Data-processing : MOV, ADD, CMP ...) ->111000 *● Puis indicateur d'operande immediat (I) ->1110001
  • 32. *
  • 33. MOV r5,#7 *● C'est une operation Move =>Opcode *->11100011101 *● Puis indicateur ''pas de mise a jour *des codes conditions'' (pas de S en suffixe) *->111000111010 *● MOV : pas de 1er operande (Rn a 0) *->1110001110100000 *● Registre destination R5 (Rd code pour 5) *->11100011101000000101 *Format immediat : constante 8 bits et rotation *constante = 0b00000111(binaire)=7décimal *● Pas besoin de deplacer la constante 8 bits *pour obtenir la valeur immediate (rotation 0) *->111000111010000001010000 *● Constante 8 bits *->11100011101000000101000000000111
  • 35. .data msg: .ascii "Hello, Piday!n" len = . ‐ msg .text .globl _start _start: /* syscall write(int fd, const void *buf, size_t count) needs 3 argument*/ mov %r0, $1 /* fd ‐> stdout standard output*/ ldr %r1, =msg /* buf ‐> msg */ ldr %r2, =len /* count ‐> len(msg) */ mov %r7, $4 /* write is syscall #4 */ swi $0 /* invoke syscall */ /* syscall exit(int status) */ mov %r0, $0 /* status ‐> 0 */ mov %r7, $1 /* exit is syscall #1 */ swi $0 /* invoke syscall */ The C function s, including the ISO C standard ones, are widely used by programs, and are regarded as if they were not only an implementation of something in the C language, but also de fact o part of the operating system interface.=> glibc Making call using C lib it is rather unusual to perform system calls directly. It is almost always preferable to call the C library instead. *
  • 36. *To terminate a program *MOV R7, #1 *SVC 0 6 of 23 *The number 1 placed in Register 7 tells the operating system to terminate this program. The instruction “SVC 0” is the system call, that transfers the program execution to the operating system. If you place a different number in R7, the operating system will perform a difference service. on ARM, the system call identifier is put in register R7, arguments are passed in R0R6 (respecting “EABI arrangement” where appropriate,i.e. 64bit arguments), and the kernel is called with the ‘SWI 0’ instruction.
  • 37. Now, coming to Raspberry Pi, which is a Broadcom SOC,BCM 2835,based on ARM Processor. Every System Call is Index in the System Call Table. The Index is an Integer value which is passed to the Register R7, in case Platform. The registers, R0, R1 and R2 are used to pass the arguments of the System Call. The instruction, SWI, now being used as SVC, which is a Supervisor Call, used to jump to the Privileged Mode, to invoke the Kernel. The embedded with SVC #num, is used to refer to the Handler. svc #0 Hence, as an example, say, we want to invoke a System Call to print "Hello Worldn". The System Call Index 'Write' is #4. Thus, the code will be something like,
  • 38. * In Linux ARM we can perform a system call by using the instruction swi. This instruction means software interruption and its sole purpose is to make a system call to the operating system. Linux we will always use swi #0 to perform a system call. No system call in Linux receives more than 7 arguments and the arguments are passed in registers r0 to r6. If the system call returns some value it will be returned in register r0.
  • 39. Hello world, the system call way As a simple illustration of calling the operating system we will write the archetypical “Hello world” program using system calls. In this case we will call the function write. Write receives three parameters: a file descriptor where we will write some data, a pointer to the data that will be written and the size of such data. Of these three, the most obscure may be now the file descriptor. Without entering into much details, it is just a number that identifies a file assigned to the process. Processes usually start with three preassigned files: the standard input, with the number 0, the standard output, with the number 1, and the standard error, with the number 2. We will write our messages to the standard output, so we will use the file descriptor 1.
  • 40.
  • 41. .arch armv6 .section .rodata .align 2 .data HelloWorldString: .ascii "Hello Worldn" .LC0: .text .align 2 .global main .type main, %function main: mov r7, #4 mov r0, #1 ldr r1,=HelloWorldString mov r2, #12 svc #0 @ Need to exit the program mov r7, #1 mov r0, #0 svc #0 .L3: .align 3 .L2: .size main, .main .ident "GCC: (Debian 4.6.314+ rpi1) 4.6.3" .section .note.GNUstack,"",% progbits Compile and run , ./program Now, check 'dmesg' using, #dmesg | tail
  • 42. Write assembly in .s file As ‐o hello.o hello.S (output object file from assembly file) Ld ‐s ‐o hello hello.o (output exeutable file) gcc ‐o hello hello.o *Linker: Finally, the linker ﴾ld/ld.exe﴿ links the object code with the library code to produce an executable file « hello/hello.exe". *> ld ‐o hello.exe hello.o ...libraries...
  • 43. * "ldd" Utility ‐ List Dynamic‐Link Libraries The utility "ldd" examines an executable and displays a list of the shared libraries that it needs. For example, > ldd hello.exe ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000) kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77600000) KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x75fa0000)
  • 44. Gdb Debugger >> gdb hello >> gdb start >> gdb disassemble *Debugging : * as gstabs o filename.o filename.s =>get assambly * If you want to use gdb, you need to invoke the assembler with some additional options. * When gdb starts, we need to set a breakpoint. * The execution of the program will stop there and we can step forwards one instruction at a time from * that point. Here, I am setting the breakpoint * at the _start label. * (gdb) break *_start * (gdb) run * (gdb) info rgisters * GDB has the ability of disassembling the machine code back to assembly instructions. The command is “disassemble”. Gdb layout asm Gdb si (stepinto) * To start the program, use command “run”*
  • 45. * *Install Code::Blocks IDE *To install Code::Blocks IDE, use the following command at the command prompt and all the required software will be installed. *$ sudo apt-get install codeblocks
  • 46. *The assembly code generated from the C code is different from the basic Assembly code.s A compiler has to produce working machine code for the infinite number of programs that can be written in the language it compiles. It is impossible to ensure that all possible highlevel Instructions are translated in the optimum way; Call assembly into C
  • 47. *
  • 48. * How a Raspberr y-Pi processor boots. The BCM2385 includes a GPU and this GPU includes a bootloader . The bootloader is capabable of reading the contents of a FAT32 partition on an SD card and booting fr om the kernel .img file contained on it. This kernel.img file is an ARM executable, and is generally the linux kernel . All we need to do is generate our executable and replace the kernel .img file on the SD card with our file to execute it. The first thing we will need to setup is the GPIO controller. There are no drivers we can rely on as there is no OS running, all the bootloader has done is boot the processor into a working state, ready to start loading the OS.
  • 49. *Now that you have extracted the template, create a new file in the 'source' directory called 'main.s'. This file will contain the code for this operating system. To be explicit, the folder structure should look like: *build/ (empty) * source/ main.s *kernel.ld *LICENSE *Makefile Open 'main.s' in a text editor so that we can begin typing assembly code. The Raspberry Pi uses a variety of assembly code called ARMv6, so that is what we'll need to write in. Copy in these first commands. .section .init .globl _start _start: *
  • 50. *This will turn on the LED and blink To install your operating system, first of all get a Raspberry PI SD card which has an operating system installed already. If you browse the files in the SD card, you should see one called kernel.img. Rename this file to something else, such as kernel_linux.img. Then, copy the file kernel.img that make generated onto the SD Card. You've just replaced the existing operating system with your own. To switch back, simply delete your kernel.img file, and rename the other one back to kernel.img. I find it is always helpful to keep a backup of you original Raspberry Pi operating system, in case you need it again.
  • 51. BareMetal OS, a 64-bit operating system written entirely in assembly. APOTHEMEOS A Small Assembly Opensource Os.
  • 52. * *Cambridge tutorials by lighting the OK LED on the Raspberry-Pi board. *Any request : Bellaj1@gmail.com
  • 53. *

Notas do Editor

  1. Compiled languages Interpreted languages P-code languages hybrid language that uses both compilation and interpretation Important p-code languages include Python, Perl, and Java.
  2. These two functions are implemented in the C library, so they are more or less supported in any environment supporting the C language.
  3. La compiltion =?
  4. GNU Compiler Collection ﴾GCC﴿: a compiler suit that supports many languages, such as C/C++, Objective‐C and Java.
  5. What is compilation?
  6. BCM2836=> pi2 BCM2835=> olds pis Docu du 35 est disponible !=36
  7. Br = branch =like jump
  8.  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. strh reg,[dest] stores the low half word number in reg at the address given by dest.
  9. ARM instruction has 32 bits in which to encode the instruction type, condition, operands etc. In group one instructions there are twelve bits available to encode immediate operands. Twelve bits of binary can represent numbers in the range 0..4095, or 2048..+ 2047 if we treat them as signed.
  10. calling the system call through the C library was not harder than calling a normal function. Let’s try the same directly performing a Linux system call. First we have to identify the number of the system call and put it in r7. The call write has the number 4 (you can see the numbers in the file /usr/include/arm‐linux‐gnueabihf/asm/unistd.h). The parameters are usually the same as in the C function, so we will use registers r0, r1 and r2 likewise.
  11. You could register your own sys call
  12. /************* CODE SECTION *************/ .text @ the following is executable assembly @ Ensure code section is 4-byte aligned: .balign 4 @ main is the entry point and must be global .global main B main @ begin at main /************* MAIN SECTION *************/ main: MOV r4, #13 @ load nth fibonacci number here MOV r0, #0 MOV r1, #1 @ Load first two fibonacci numbers loop: B loop done: BX lr @ exit cleanly .end @ end of code ///////////////////// As you can see it is not that different to a function call but instead of branching to a specific address of code using bl we use swi #0. Truth be told, it is rather unusual to perform system calls directly. It is almost always preferable to call the C library instead.
  13. Hidden slid
  14. Use time to compare the difference between native Assambly code execution and the execution of the assembly code generated from C code.
  15. Un os en assembly projet, Almost all Unix code except a small amount of direct hardware-interface support in the kernel itself is nowadays written in a high-level language.
  16. ARM instruct and thmb2