C from hello world to 010101

Bellaj Badr
Bellaj BadrCTO at Mchain | Blockchain consultant em Mchain
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
*
C from hello world to 010101
C from hello world to 010101
*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
C from hello world to 010101
*
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)
C from hello world to 010101
*
*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.
C from hello world to 010101
.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
*
1 de 53

Recomendados

Assembler por
AssemblerAssembler
AssemblerMohd Arif
9.8K visualizações25 slides
Assembler por
AssemblerAssembler
AssemblerJad Matta
549 visualizações22 slides
Microprocessor chapter 9 - assembly language programming por
Microprocessor  chapter 9 - assembly language programmingMicroprocessor  chapter 9 - assembly language programming
Microprocessor chapter 9 - assembly language programmingWondeson Emeye
46.8K visualizações30 slides
Lecture5(1) por
Lecture5(1)Lecture5(1)
Lecture5(1)misgina Mengesha
471 visualizações21 slides
Organization of the ibm personal computers por
Organization of the ibm personal computersOrganization of the ibm personal computers
Organization of the ibm personal computerswarda aziz
4.6K visualizações4 slides
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ... por
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Bilal Amjad
745 visualizações31 slides

Mais conteúdo relacionado

Mais procurados

Examinable Question and answer system programming por
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programmingMakerere university
14K visualizações82 slides
Lecture6 por
Lecture6Lecture6
Lecture6misgina Mengesha
573 visualizações12 slides
Part III: Assembly Language por
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly LanguageAhmed M. Abed
1.8K visualizações27 slides
MASM -UNIT-III por
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-IIIDr.YNM
21.8K visualizações13 slides
Assembly language programming_fundamentals 8086 por
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
29.3K visualizações49 slides
Assembly 8086 por
Assembly 8086Assembly 8086
Assembly 8086Mustafa Salah
21.7K visualizações105 slides

Mais procurados(20)

Examinable Question and answer system programming por Makerere university
Examinable Question and answer system programmingExaminable Question and answer system programming
Examinable Question and answer system programming
Makerere university14K visualizações
Lecture6 por misgina Mengesha
Lecture6Lecture6
Lecture6
misgina Mengesha573 visualizações
Part III: Assembly Language por Ahmed M. Abed
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly Language
Ahmed M. Abed1.8K visualizações
MASM -UNIT-III por Dr.YNM
MASM -UNIT-IIIMASM -UNIT-III
MASM -UNIT-III
Dr.YNM 21.8K visualizações
Assembly language programming_fundamentals 8086 por Shehrevar Davierwala
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
Shehrevar Davierwala29.3K visualizações
Assembly 8086 por Mustafa Salah
Assembly 8086Assembly 8086
Assembly 8086
Mustafa Salah21.7K visualizações
Chapter 2 programming concepts - I por SHREEHARI WADAWADAGI
Chapter 2  programming concepts - IChapter 2  programming concepts - I
Chapter 2 programming concepts - I
SHREEHARI WADAWADAGI577 visualizações
Introduction to debugging linux applications por commiebstrd
Introduction to debugging linux applicationsIntroduction to debugging linux applications
Introduction to debugging linux applications
commiebstrd865 visualizações
Assembler por Maha Lakshmi
AssemblerAssembler
Assembler
Maha Lakshmi12.3K visualizações
Assembler design option por Mohd Arif
Assembler design optionAssembler design option
Assembler design option
Mohd Arif6K visualizações
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING por Frankie Jones
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Frankie Jones66.4K visualizações
Programming ATmega microcontroller using Embedded C por Varun A M
Programming ATmega microcontroller using Embedded CProgramming ATmega microcontroller using Embedded C
Programming ATmega microcontroller using Embedded C
Varun A M47.5K visualizações
06 - ELF format, knowing your friend por Alexandre Moneger
06 - ELF format, knowing your friend06 - ELF format, knowing your friend
06 - ELF format, knowing your friend
Alexandre Moneger2.3K visualizações
Introduction to Assembly Language por ApekshaShinde6
Introduction to Assembly Language Introduction to Assembly Language
Introduction to Assembly Language
ApekshaShinde682 visualizações
Something About Dynamic Linking por Wang Hsiangkai
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
Wang Hsiangkai2.2K visualizações
It322 intro 3 por J Cza Àkera
It322 intro 3It322 intro 3
It322 intro 3
J Cza Àkera285 visualizações
Binary art - Byte-ing the PE that fails you (extended offline version) por Ange Albertini
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)
Ange Albertini23.8K visualizações
Assembly language part I por Mohammed A. Imran
Assembly language part IAssembly language part I
Assembly language part I
Mohammed A. Imran3.5K visualizações
POWER processor and features presentation por Ganesan Narayanasamy
POWER processor and features presentationPOWER processor and features presentation
POWER processor and features presentation
Ganesan Narayanasamy115 visualizações

Similar a C from hello world to 010101

Introduction to computer architecture .pptx por
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptxFatma Sayed Ibrahim
8 visualizações67 slides
Report on c and c++ por
Report on c and c++Report on c and c++
Report on c and c++oggyrao
138 visualizações70 slides
C tutorials por
C tutorialsC tutorials
C tutorialsAmit Kapoor
1.9K visualizações88 slides
ISA.pptx por
ISA.pptxISA.pptx
ISA.pptxFarrukhMuneer2
6 visualizações26 slides
Lecture 1 por
Lecture 1Lecture 1
Lecture 1marvellous2
131 visualizações20 slides
NASM Introduction.pptx por
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptxAnshKarwa
34 visualizações20 slides

Similar a C from hello world to 010101(20)

Introduction to computer architecture .pptx por Fatma Sayed Ibrahim
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptx
Fatma Sayed Ibrahim8 visualizações
Report on c and c++ por oggyrao
Report on c and c++Report on c and c++
Report on c and c++
oggyrao138 visualizações
C tutorials por Amit Kapoor
C tutorialsC tutorials
C tutorials
Amit Kapoor1.9K visualizações
ISA.pptx por FarrukhMuneer2
ISA.pptxISA.pptx
ISA.pptx
FarrukhMuneer26 visualizações
Lecture 1 por marvellous2
Lecture 1Lecture 1
Lecture 1
marvellous2131 visualizações
NASM Introduction.pptx por AnshKarwa
NASM Introduction.pptxNASM Introduction.pptx
NASM Introduction.pptx
AnshKarwa34 visualizações
Assembler Programming por Omar Sanchez
Assembler ProgrammingAssembler Programming
Assembler Programming
Omar Sanchez491 visualizações
C programming session10 por Keroles karam khalil
C programming  session10C programming  session10
C programming session10
Keroles karam khalil2.7K visualizações
Embedded C programming session10 por Keroles karam khalil
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
Keroles karam khalil1.2K visualizações
Writing c code for the 8051 por Quản Minh Tú
Writing c code for the 8051Writing c code for the 8051
Writing c code for the 8051
Quản Minh Tú21.6K visualizações
07 processor basics por Murali M
07 processor basics07 processor basics
07 processor basics
Murali M63 visualizações
Course Materials por Shelly Martinez
Course MaterialsCourse Materials
Course Materials
Shelly Martinez2 visualizações
Issues in the design of Code Generator por Darshan sai Reddy
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
Darshan sai Reddy1.8K visualizações
Fp201 unit2 1 por rohassanie
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
rohassanie536 visualizações
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010] por RootedCON
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]
RootedCON1.2K visualizações
C Under Linux por mohan43u
C Under LinuxC Under Linux
C Under Linux
mohan43u1.6K visualizações
Embedded system (Chapter 2) part 2 por Ikhwan_Fakrudin
Embedded system (Chapter 2) part 2Embedded system (Chapter 2) part 2
Embedded system (Chapter 2) part 2
Ikhwan_Fakrudin5.6K visualizações
Lect05 Prog Model por anoosdomain
Lect05 Prog ModelLect05 Prog Model
Lect05 Prog Model
anoosdomain361 visualizações
20 -miscellaneous por Hector Garzo
20  -miscellaneous20  -miscellaneous
20 -miscellaneous
Hector Garzo257 visualizações
X86 assembly nasm syntax por Francesco DiFusco
X86 assembly nasm syntaxX86 assembly nasm syntax
X86 assembly nasm syntax
Francesco DiFusco1.5K visualizações

Mais de Bellaj Badr

Cours4.pptx por
Cours4.pptxCours4.pptx
Cours4.pptxBellaj Badr
6 visualizações37 slides
0240-formation-ssh-secure-shell.pdf por
0240-formation-ssh-secure-shell.pdf0240-formation-ssh-secure-shell.pdf
0240-formation-ssh-secure-shell.pdfBellaj Badr
7 visualizações44 slides
5-Authentification.2P.pdf por
5-Authentification.2P.pdf5-Authentification.2P.pdf
5-Authentification.2P.pdfBellaj Badr
8 visualizações15 slides
Is web 3 an overengineered solution por
Is web 3 an overengineered solutionIs web 3 an overengineered solution
Is web 3 an overengineered solutionBellaj Badr
114 visualizações28 slides
create your own cryptocurrency por
create your own cryptocurrencycreate your own cryptocurrency
create your own cryptocurrencyBellaj Badr
527 visualizações52 slides
Blockchain and bitcoin in numbers por
Blockchain and bitcoin in numbersBlockchain and bitcoin in numbers
Blockchain and bitcoin in numbersBellaj Badr
328 visualizações43 slides

Mais de Bellaj Badr(14)

Cours4.pptx por Bellaj Badr
Cours4.pptxCours4.pptx
Cours4.pptx
Bellaj Badr6 visualizações
0240-formation-ssh-secure-shell.pdf por Bellaj Badr
0240-formation-ssh-secure-shell.pdf0240-formation-ssh-secure-shell.pdf
0240-formation-ssh-secure-shell.pdf
Bellaj Badr7 visualizações
5-Authentification.2P.pdf por Bellaj Badr
5-Authentification.2P.pdf5-Authentification.2P.pdf
5-Authentification.2P.pdf
Bellaj Badr8 visualizações
Is web 3 an overengineered solution por Bellaj Badr
Is web 3 an overengineered solutionIs web 3 an overengineered solution
Is web 3 an overengineered solution
Bellaj Badr114 visualizações
create your own cryptocurrency por Bellaj Badr
create your own cryptocurrencycreate your own cryptocurrency
create your own cryptocurrency
Bellaj Badr527 visualizações
Blockchain and bitcoin in numbers por Bellaj Badr
Blockchain and bitcoin in numbersBlockchain and bitcoin in numbers
Blockchain and bitcoin in numbers
Bellaj Badr328 visualizações
Blockchain Tokenization por Bellaj Badr
Blockchain TokenizationBlockchain Tokenization
Blockchain Tokenization
Bellaj Badr994 visualizações
Security in the blockchain por Bellaj Badr
Security in the blockchainSecurity in the blockchain
Security in the blockchain
Bellaj Badr3.5K visualizações
Blockchain demystification por Bellaj Badr
Blockchain demystificationBlockchain demystification
Blockchain demystification
Bellaj Badr2.8K visualizações
Bitcoin por Bellaj Badr
BitcoinBitcoin
Bitcoin
Bellaj Badr1.1K visualizações
An introduction to AI (artificial intelligence) por Bellaj Badr
An introduction to AI (artificial intelligence)An introduction to AI (artificial intelligence)
An introduction to AI (artificial intelligence)
Bellaj Badr1.8K visualizações
Connected Car Platform (CC-p) por Bellaj Badr
Connected Car Platform (CC-p) Connected Car Platform (CC-p)
Connected Car Platform (CC-p)
Bellaj Badr779 visualizações
the age of cryptocurrency at Devoxx Morocco por Bellaj Badr
the age of cryptocurrency at Devoxx  Moroccothe age of cryptocurrency at Devoxx  Morocco
the age of cryptocurrency at Devoxx Morocco
Bellaj Badr2K visualizações
beware of Thing Bot por Bellaj Badr
beware of Thing Botbeware of Thing Bot
beware of Thing Bot
Bellaj Badr1.1K visualizações

Último

DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... por
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...Deltares
17 visualizações12 slides
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... por
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Donato Onofri
795 visualizações34 slides
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports por
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsRa'Fat Al-Msie'deen
5 visualizações49 slides
Software evolution understanding: Automatic extraction of software identifier... por
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Ra'Fat Al-Msie'deen
7 visualizações33 slides
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with... por
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...sparkfabrik
5 visualizações46 slides
AI and Ml presentation .pptx por
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptxFayazAli87
11 visualizações15 slides

Último(20)

DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... por Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 visualizações
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... por Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri795 visualizações
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports por Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Ra'Fat Al-Msie'deen5 visualizações
Software evolution understanding: Automatic extraction of software identifier... por Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Ra'Fat Al-Msie'deen7 visualizações
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with... por sparkfabrik
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
sparkfabrik5 visualizações
AI and Ml presentation .pptx por FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8711 visualizações
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... por Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 visualizações
SAP FOR CONTRACT MANUFACTURING.pdf por Virendra Rai, PMP
SAP FOR CONTRACT MANUFACTURING.pdfSAP FOR CONTRACT MANUFACTURING.pdf
SAP FOR CONTRACT MANUFACTURING.pdf
Virendra Rai, PMP11 visualizações
HarshithAkkapelli_Presentation.pdf por harshithakkapelli
HarshithAkkapelli_Presentation.pdfHarshithAkkapelli_Presentation.pdf
HarshithAkkapelli_Presentation.pdf
harshithakkapelli11 visualizações
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... por Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 visualizações
Agile 101 por John Valentino
Agile 101Agile 101
Agile 101
John Valentino7 visualizações
SAP FOR TYRE INDUSTRY.pdf por Virendra Rai, PMP
SAP FOR TYRE INDUSTRY.pdfSAP FOR TYRE INDUSTRY.pdf
SAP FOR TYRE INDUSTRY.pdf
Virendra Rai, PMP24 visualizações
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx por animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm14 visualizações
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols por Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares7 visualizações
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema por Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 visualizações
The Era of Large Language Models.pptx por AbdulVahedShaik
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptx
AbdulVahedShaik5 visualizações
Software testing company in India.pptx por SakshiPatel82
Software testing company in India.pptxSoftware testing company in India.pptx
Software testing company in India.pptx
SakshiPatel827 visualizações
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h... por Deltares
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
DSD-INT 2023 Exploring flash flood hazard reduction in arid regions using a h...
Deltares5 visualizações
MariaDB stored procedures and why they should be improved por Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
Federico Razzoli8 visualizações
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs por Deltares
DSD-INT 2023 The Danube Hazardous Substances Model - KovacsDSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
Deltares8 visualizações

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. *
  • 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
  • 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)
  • 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.
  • 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