initial import
[staff/goc4/2018bti7061.git] / asm-5-programming / examples / stack / stack.asm
1 ; Executable name : stack
2 ; Version : 1.0
3 ; Created date : September 2, 2016
4 ; Last update : September 2, 2016
5 ; Author : Emmanuel Benoist
6 ; Description : A simple program in assembly for Linux, using NASM,
7 ; demonstrating the use of Linux 64-bit syscalls to display text.
8 ;
9 ; Build using these commands:
10 ; nasm -f elf64 -g stack.asm
11 ; ld -o stack stack.o
12 ; Or use make to compile
13 ; make stack
14
15 SECTION .data ; Section containing initialised data
16 vars1: dq 1,2,3,4,5 ; contains an array of numbers
17 ; (on 8 bytes qwords)
18 var2: db "Hello World",10 ; 10 stands for \n
19
20
21 SECTION .bss ; Section containing uninitialized data
22
23 SECTION .text ; Section containing code
24
25
26 global _start ; Linker needs this to find the entry point!
27
28 _start:
29 nop ; This no-op keeps gdb happy.
30 mov rax,10 ;Copy the value 10 (=0Ah) in RAX
31 mov rbx, vars1
32 mov edx, var2
33 ; pusha ; Not supported in 64-bit mode
34 ; pushad ; Not supported in 64-bit mode
35 push ax ; Push the AX register
36 ; push ebx ; push the EAX register not supported in 64-bit mode
37 push rax ; Push the RAX register
38 push word[rbx] ; Push the word stored at rbx
39 push qword[rdx] ; Push the quad word stored at rdx
40 push rdi ; Push the RDI register
41
42 pushf ; Push the flags on the stack
43
44 nop ;See the memory in the debugger
45
46 popf ;Pop the flags out of the stack
47
48 pop CX ; Pop the top 2 bytes from the stack into CX
49 pop RSI ; Pop the top 16 bytes from the stack into RSI
50 pop qword[rbx] ; Pop the top 8 bytes from the stack into the memory at EBX
51 pop qword[vars1] ; Pop the top 8 bytes from the stack into the memory at var1
52
53
54
55
56
57
58 ;;; Normal exit of the program (to finish properly)
59 nop
60 mov rax, 60 ; Code for exit
61 mov rdi, 0 ; Return a code of zero
62 syscall ; Make kernel call
63
64
65