|
1 ;; |
|
2 ;; hello.asm |
|
3 ;; Basics in Assembler |
|
4 ;; |
|
5 global start |
1 global _entryPoint |
6 global _entryPoint |
2 |
7 |
3 %define SYSCALL_EXIT 0x1 |
8 %define SYSCALL_EXIT 0x1 |
4 %define SYSCALL_WRITE 0x4 |
9 %define SYSCALL_WRITE 0x4 |
|
10 %define SYS_INTERRUPT 0x80 |
5 |
11 |
6 section .data |
12 section .data |
7 msg db "Hello World!", 0x0a ; Die Nachricht |
13 msg db "ASM Tutorial for 32 Bit Intel!", 10, 0 |
8 len equ $-msg |
14 len equ $-msg |
|
15 |
|
16 buffer: times 100 db '.' |
|
17 quotient: dd 10 |
9 |
18 |
10 section .text |
19 section .text |
11 |
20 |
12 call _entryPoint |
21 start: |
13 jmp asm_exit |
22 call _entryPoint |
|
23 mov eax, 1000 |
|
24 mov ebx, -1024 |
|
25 call calculate |
|
26 |
|
27 mov eax, 1000 |
|
28 mov ebx, 1024 |
|
29 call calculate |
|
30 |
|
31 call asm_exit |
|
32 ret |
|
33 |
|
34 asm_exit: |
|
35 push DWORD 0 |
|
36 mov eax, SYSCALL_EXIT |
|
37 int SYS_INTERRUPT |
|
38 ret |
|
39 |
|
40 asm_write: |
|
41 mov eax, SYSCALL_WRITE |
|
42 int SYS_INTERRUPT |
|
43 ret |
|
44 |
|
45 ;; |
|
46 ;; CL: NEGATIVE BIT - BOOLEAN |
|
47 ;; EAX: NUMBER TO CONVERT |
|
48 ;; EBX: counter |
|
49 ;; |
|
50 integerToString: |
|
51 xor cl, cl |
|
52 |
|
53 mov ebx, 0 |
|
54 lea si, [buffer] |
|
55 mov di, si |
|
56 |
|
57 cmp eax, 0 |
|
58 jg loop |
|
59 |
|
60 ;; SET NEGATIVE |
|
61 mov cl, 1 |
|
62 |
|
63 ;; reverse internal negative representation |
|
64 not eax |
|
65 inc eax |
|
66 |
|
67 loop: |
|
68 xor edx, edx |
|
69 idiv DWORD [quotient] |
|
70 add edx, 30h |
|
71 mov [si], edx |
|
72 inc si |
|
73 inc ebx |
|
74 |
|
75 cmp eax, 0 |
|
76 jne loop |
|
77 |
|
78 ;; Save current string position |
|
79 push si |
|
80 |
|
81 ;; ADD +/- SIGN |
|
82 cmp cl, 0 |
|
83 jz positive |
|
84 jg negative |
|
85 |
|
86 positive: |
|
87 mov BYTE [si], '+' |
|
88 jmp reverse |
|
89 |
|
90 negative: |
|
91 mov BYTE [si], '-' |
|
92 jmp reverse |
|
93 |
|
94 ;; Reverse the number in human readable format |
|
95 reverse: |
|
96 ;; swap first with last and so on |
|
97 mov al, BYTE [si] |
|
98 mov ah, BYTE [di] |
|
99 mov BYTE [si], ah |
|
100 mov BYTE [di], al |
|
101 |
|
102 dec si |
|
103 inc di |
|
104 |
|
105 ;; 1 2 3 4 5 6 |
|
106 ;; 6 1 |
|
107 ;; 5 2 |
|
108 ;; 4 3 |
|
109 ;; |
|
110 cmp si, di |
|
111 jg reverse |
|
112 |
|
113 ;; Restore current String position |
|
114 pop di |
|
115 |
|
116 ;; add newline and string terminator |
|
117 newline: |
|
118 mov BYTE [di], 10 |
|
119 inc di |
|
120 mov BYTE [di], 0 |
|
121 inc di |
|
122 |
|
123 ;; add the two chars to the counter |
|
124 add ebx, 2 |
|
125 |
|
126 ret |
|
127 |
|
128 calculate: |
|
129 imul ebx |
|
130 call integerToString |
|
131 |
|
132 push DWORD ebx |
|
133 push DWORD buffer |
|
134 push DWORD 1 |
|
135 |
|
136 call asm_write |
|
137 |
|
138 add esp, 12 |
|
139 ret |
14 |
140 |
15 _entryPoint: |
141 _entryPoint: |
16 push dword len ;; Länge des Texts |
142 push DWORD len ;; Text Len |
17 push dword msg ;; Der Text |
143 push DWORD msg ;; Pointer to String |
18 push dword 1 ;; stdout |
144 push DWORD 1 ;; stdout |
19 |
145 |
20 ;; call write |
146 call asm_write ;; |
21 mov eax, SYSCALL_WRITE |
|
22 sub esp, 4 |
|
23 int 0x80 |
|
24 |
147 |
25 ;; clean up 3 pushes |
148 add esp, 12 ;; cleanup stack |
26 add esp, 16 |
149 ret |
27 |
|
28 asm_exit: |
|
29 push dword 0 |
|
30 mov eax, SYSCALL_EXIT |
|
31 sub esp, 4 |
|
32 int 0x80 |
|