Assembly x86-32

IN DEVELOPMENT...

Assembly x86-64

How to compile these examples: gcc main.c func.s
###########################################################################
# f000.s
.text
.globl f000

f000:
movl $12345, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
// main000.c
#include <stdio.h>
int f000(void);
void main(void)
{
int m;
m=f000();
printf("%d\n", m);

}
###########################################################################
.text
.globl f003

f003:
movl %ecx, %eax
imull %ecx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f003(int);
void main(void)
{
int k=1213, m;
m=f003(k);
printf("%d\n", m);

}
###########################################################################
.text
.globl f005

f005:
addss %xmm1, %xmm0
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
float f005(float, float);
void main(void)
{
float fa, fb, fc;
fa=1.123456;
fb=6.778899;
fc=f005(fa, fb);
printf("%f + %f = %f\n", fa, fb, fc);

}
###########################################################################
.text
.globl f007

f007:
movl $0, %eax
loop:
addl %ecx, %eax
decl %ecx
jnz loop
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f007(int);
void main(void)
{
int k=100, m;
m=f007(k);
printf("sum of first %d integers = %d\n", k, m);

}
###########################################################################
# sum of %ecx and %edx
.text
.globl f009

f009:
movl %ecx, %eax
addl %edx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f009(int, int);
void main(void)
{
int k=120, m=155, n;
n=f009(k, m);
printf("%d + %d = %d\n", k, m, n);

}
###########################################################################
.text
.globl f012

f012:
movl %ecx, %eax
cmpl %edx, %eax
jg .L2
movl %ecx, %eax
jmp .L3

.L2:
movl %edx, %eax
.L3:
ret
----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f012(int, int);
void main(void)
{
int k=120, m=155, n;
n=f012(k, m);
printf("%d , %d min = %d\n", k, m, n);

}
###########################################################################
.text
.globl f015

f015:
movq %rcx, %rax
imulq %rdx, %rax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
long long f015(long long, long long);
void main(void)
{
long long k=120123, m=999999, n;
n=f015(k, m);
printf("%lld * %lld = %lld\n", k, m, n);

}

###########################################################################
.text
.globl f017

f017:
movq %rcx, %rax
movq %rdx, %r9
cqto # sign extend rax to rdx:rax
idivq %r9
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
long long f017(long long, long long);
void main(void)
{
long long k=12201230123, m=999999, n;
n=f017(k, m);
printf("%lld / %lld = %lld\n", k, m, n);

}
###########################################################################
.text
.globl f018

f018:
movq %rcx, %rax
movq %rdx, %r9
cqto # sign extend rax to rdx:rax
idivq %r9
movq %rdx, %rax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
long long f018(long long, long long);
void main(void)
{
long long k=12201230123, m=999999, n;
n=f018(k, m);
printf("%lld residual %lld = %lld\n", k, m, n);

}
###########################################################################
.text
.globl f020

f020:
movl %ecx, %eax
andl %edx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f020(int, int);
void main(void)
{
int k=122, m=999999, n;
n=f020(k, m);
printf("%x & %x = %x\n", k, m, n);

}
###########################################################################
.text
.globl f021

f021:
movl %ecx, %eax
orl %edx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f021(int, int);
void main(void)
{
int k=122, m=999999, n;
n=f021(k, m);
printf("%x | %x = %x\n", k, m, n);

}
###########################################################################
.text
.globl f022

f022:
movl %ecx, %eax
notl %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f022(int);
void main(void)
{
int m=999999, n;
n=f022(m);
printf(" ~%x = %x\n", m, n);

}
###########################################################################
.text
.globl f023

f023:
sall $3, %ecx
movl %ecx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f023(int);
void main(void)
{
int m=999999, n;
n=f023(m);
printf(" %x <<3 = %x\n", m, n);

}
###########################################################################
.text
.globl f024

f024:
sarl $3, %ecx
movl %ecx, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f024(int);
void main(void)
{
int m=999999, n;
n=f024(m);
printf(" %x >>3 = %x\n", m, n);

}
###########################################################################
.text
.globl f027

f027:
cmpl $0, %ecx
je .L2
cmpl $0, %edx
je .L2
movl $1, %eax
jmp .L3

.L2:
movl $0, %eax
.L3:
ret
----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f027(int, int);
void main(void)
{
int k=1, m=2, n;
n=f027(k, m);
printf("%d && %d = %d\n", k, m, n);

}
###########################################################################
.text
.globl f028

f028:
cmpl $0, %ecx
jne .L2
cmpl $0, %edx
je .L3

.L2:
movl $1, %eax
jmp .L4

.L3:
movl $0, %eax
.L4:
ret
----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
int f028(int, int);
void main(void)
{
int k=1, m=0, n;
n=f028(k, m);
printf("%d || %d = %d\n", k, m, n);

}
###########################################################################
.text
.globl f050

f050:
movq %rcx, %rdi
movl $0, %esi

.L2:
movl %esi, %edx
movq %rdi, %rax
addq %rdx, %rax
movzbl (%rax), %eax
testb %al, %al
je .L3
incl %esi
jmp .L2

.L3:
movl %esi, %eax
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
unsigned int f050(char*);
void main(void)
{
char buf[50] = {'a', 'b', 'c', 'd', 'e', 'f', 0};
int n;
n=f050(buf);
printf("length = %d\n", n);

}
###########################################################################
.text
.globl f080

f080:
addsd %xmm1, %xmm0
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
double f080(double, double);
void main(void)
{
double fa, fb, fc;
fa=1.123456;
fb=6.778899;
fc=f080(fa, fb);
printf("%f + %f = %f\n", fa, fb, fc);

}
###########################################################################
.text
.globl f082

f082:
mulsd %xmm1, %xmm0
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
double f082(double, double);
void main(void)
{
double fa, fb, fc;
fa=1.123456;
fb=6.778899;
fc=f082(fa, fb);
printf("%f * %f = %f\n", fa, fb, fc);

}
###########################################################################
.text
.globl f084

f084:
divsd %xmm1, %xmm0
ret

----------------------------------------------------- ----------------------------------------------------------
#include <stdio.h>
double f084(double, double);
void main(void)
{
double fa, fb, fc;
fa=1.123456;
fb=6.778899;
fc=f084(fa, fb);
printf("%f / %f = %f\n", fa, fb, fc);

}

Pure Assembly

Here are programs purely in Assembly that aren't compiled into executables with c code.
To compile these programs: gcc prog.s
###########################################################################
# simplest assembly program
# which is compiled and run

.text
.globl main

main:
ret
###########################################################################
# simplest assembly program
# which is compiled and run

.text
.globl main

main:
call __main # call next instruction
nop
ret

###########################################################################
# string definition and print

.text
.globl main

main:
pushq %rbp
movq %rsp, %rbp
subq $24, %rsp # reserve frame in stack for variables
call __main

leaq .LC0(%rip), %rcx
call puts

addq $24, %rsp
popq %rbp
ret

.LC0:
.ascii "ABCDEFGHIJK\0"
###########################################################################
.text
.globl main

main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp # reserve frame in stack for variables

call __main # call next instruction
nop

addq $32, %rsp # free frame in stack
popq %rbp
ret

###########################################################################
.section .rdata,"dr"
.LC0:
.ascii "ABCDEF\0"

.text
.globl main

main:
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp # reserve frame in stack for variables
call __main

leaq .LC0(%rip), %rcx
call puts

nop
addq $48, %rsp
popq %rbp
ret

###########################################################################
.section .rdata,"dr"
.LC0:
.ascii "%d\12\0"
.text
.globl main

main:
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp
call __main

movl $123, -4(%rbp)
movl -4(%rbp), %eax
movl %eax, %edx
leaq .LC0(%rip), %rcx
call printf

nop
addq $48, %rsp
popq %rbp
ret

###########################################################################
.text
.globl main

main:
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp
call __main
leaq -16(%rbp), %rdx
movq $123, %rax
movq $10, %r8d
movq %rax, %rcx
call itoa
leaq -16(%rbp), %rax
movq %rax, %rcx
call puts
nop
addq $48, %rsp
popq %rbp
ret

###########################################################################
.global main

.text

main: # This is called by C library's startup code
mov $message, %rdi # First integer (or pointer) parameter in %rdi
call puts # puts(message)
ret # Return to C library code

message:
.asciz "Hola, mundo" # asciz puts a 0 byte at the end