Skip to content
Snippets Groups Projects
Commit d3c37702 authored by Michal Sojka's avatar Michal Sojka
Browse files

Move examples out of tgz archives

This way, everybody can browse the source code easily online as well
as download it as a tar.
parent 7d7f946e
No related branches found
No related tags found
No related merge requests found
Showing
with 323 additions and 0 deletions
all: hello hello.static stack
hello: hello.c
$(CC) $^ -ggdb3 -o $@
# strip $@
hello.static: hello.c
$(CC) -static $^ -g -o $@
# strip $@
#include <stdio.h>
int f(int x) {
int y;
if (x>0) {
y=f(x-1)*2;
} else {
y=10;
}
return y;
}
int main() {
int a=1;
int b=2;
int c = (a&0xfe)+(b|0x11);
b=f(a);
if (c>1) {
printf("a+b je %i\n",c);
} else {
printf("ERROR\n");
}
b = f(1);
return 0;
}
all: stack-32 stack-64
stack-32: stack-32.c
$(CC) $^ -m32 -fno-stack-protector -g -o $@
stack-64: stack-64.c
$(CC) $^ -g -o $@
#include <stdio.h>
#include <unistd.h>
int b() {
printf("Co tu delam?\n");
_exit(1);
}
int main();
void f(int x) {
unsigned int local[2];
int i;
local[0]=0x111;
local[1]=0x222;
for (i=10; i>=0; i--) {
printf("%02i - %08x\n", i, local[i]);
}
local[6]=((unsigned int)&b);
printf("x=%i main %p\n", x, &main);
}
int main() {
f(10);
printf("Proc?\n");
return 0;
}
#include <stdio.h>
#include <unistd.h>
int b() {
printf("Co tu delam?\n");
_exit(1);
}
int main();
void f(int x) {
unsigned long local[2], sp;
int i;
local[0]=1;
local[1]=2;
asm volatile (
"mov %%rsp, %%rax; "
: "=a" (sp) : : );
for (i=9; i>=0; i--) {
printf("%i - %016lx\n", i, local[i]);
}
printf("main %p\n", &main);
printf("rsp %016lx\n", sp);
local[5]=(unsigned long)&b;
}
int main() {
f(10);
printf("Proc?\n");
return 0;
}
all: mov shift div
mov: mov.c
$(CC) $^ -o $@
shift: shift.c
$(CC) $^ -o $@
div: div.c
$(CC) $^ -o $@
#include <stdio.h>
int main()
{
int c_a, c_b;
c_a = -15;
c_b = -5;
asm volatile (
"mov %%eax, %%edx;"
"sar $0x1f, %%edx;"
"idivl %%ebx; "
: "+a" (c_a) : "b" (c_b) : "edx");
printf("-15/(-5)?=%i\n", c_a);
c_a = -15;
c_b = -5;
asm volatile (
"xor %%edx, %%edx;"
"divl %%ebx; "
: "+a" (c_a) : "b" (c_b) : "edx" );
printf("-15/(-5)?=%i\n", c_a);
c_a = -15;
c_b = -5;
asm volatile (
"mov %%eax, %%edx;"
"sar $0x1f, %%edx;"
"divl %%ebx; "
: "+a" (c_a) : "b" (c_b) : "edx" );
printf("-15/(-5)?=%i\n", c_a);
return 0;
}
#include <stdio.h>
int main()
{
int a;
unsigned int b;
asm volatile (
"mov $0xffff, %%eax; "
: "+a" (a) : : );
printf("a=%i\n", a);
asm volatile (
"movl $0xffff, %%ecx; "
"mov %%ecx, %%eax; "
: "+a" (a) : : "cx" );
printf("a=%i\n", a);
asm volatile (
"movw $0xffff, %%cx; "
"movswl %%cx, %%eax; "
: "+a" (a) : : "cx" );
printf("a=%i\n", a);
asm volatile (
"mov $0xffffffff, %%eax; "
: "+a" (a) : );
printf("a=%i\n", a);
asm volatile (
"mov $0xffff, %%eax; "
: "+a" (b) : : );
printf("b=%u\n", b);
asm volatile (
"movl $0xffff, %%ecx; "
"mov %%ecx, %%eax; "
: "+a" (b) : : "cx" );
printf("b=%u\n", b);
asm volatile (
"movw $0xffff, %%cx; "
"movzwl %%cx, %%eax; "
: "+a" (b) : : "cx" );
printf("b=%u\n", b);
asm volatile (
"mov $0xffffffff, %%eax; "
: "+a" (b) : );
printf("b=%u\n", b);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
unsigned int c, d;
a = -16;
asm volatile (
"shr $3, %%eax; "
: "+a" (a) : : );
printf("-16/8?=%i\n", a);
a = -16;
asm volatile (
"sar $3, %%eax; "
: "+a" (a) : : );
printf("-16/8?=%i\n", a);
return 0;
}
all: hello
hello.o: hello.S
as $^ -o $@
# objdump -d $@
hello: hello.o
ld $^ -o $@
# objdump -d $@
# strip $@
.section .rodata
greeting:
.string "Hello World\n"
.text
.global _start
_start:
mov $4,%eax /* write is syscall no. 4 */
mov $1,%ebx /* where to write (file descriptor): 1 == stdout */
mov $greeting,%ecx /* address of the data */
mov $12,%edx /* length of the data */
int $0x80 /* call the system */
../1-asm/Makefile
\ No newline at end of file
.section .rodata
greeting:
.string "Hello World\n"
.text
.global _start
_start:
mov $4,%eax /* write is syscall no. 4 */
mov $1,%ebx /* where to write (file descriptor): 1 == stdout */
mov $greeting,%ecx /* address of the data */
mov $12,%edx /* length of the data */
int $0x80 /* call the system */
mov $0xfc,%eax /* exit system call */
xorl %ebx, %ebx /* exit status set ebx to 0 */
int $0x80 /* call the system */
all: hello hello-soft
hello: hello.c
$(CC) -m32 $^ -o $@
hello-soft: hello-soft.c
$(CC) -m32 $^ -o $@
#include <unistd.h>
int main()
{
int syscall_num = 4;
int fd = 1;
char *ptr="Hello World\n";
int len = 12;
asm volatile (
"int $0x80"
: : "a" (syscall_num),
"b" (fd), "c" (ptr),
"d" (len) : "memory");
return 0;
}
#include <unistd.h>
int main()
{
asm volatile (
"int $0x80"
: : "a" (4), "b" (1), "c" ("Hello World\n"), "d" (12) : "memory");
return 0;
}
all: hello hello.static
hello: hello.c
$(CC) $^ -o $@
# strip $@
hello.static: hello.c
$(CC) -static $^ -o $@
# strip $@
#include <unistd.h>
int main()
{
write(1, "Hello world\n", 12);
return 0;
}
CC=klcc
all: hello hello.static
hello: hello.c
$(CC) $(CFLAGS) $^ -o $@
# strip $@
hello.static: hello.c
$(CC) $(CFLAGS) -static $^ -o $@
# strip $@
../3-glibc/hello.c
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment