2er Komplement und integerToString
authorMarkus Bröker<broeker.markus@googlemail.com>
Sun, 22 Oct 2017 22:55:27 +0200
changeset 172 43ae72f88d06
parent 171 c6e0af68825a
child 173 374a86886bc5
2er Komplement und integerToString
osx/Makefile
osx/hello.asm
osx/main.c
--- a/osx/Makefile
+++ b/osx/Makefile
@@ -9,7 +9,7 @@
   TARGETS+= helloC
 
 OBJECTS  = hello.o main.o
-ENTRYPOINT = _entryPoint
+ENTRYPOINT = start
 
 .SUFFIXES: .c .asm
 
--- a/osx/hello.asm
+++ b/osx/hello.asm
@@ -1,32 +1,149 @@
+;;
+;; hello.asm
+;; Basics in Assembler
+;;
+global start
 global _entryPoint
 
 %define  SYSCALL_EXIT 0x1
 %define SYSCALL_WRITE 0x4
+%define SYS_INTERRUPT 0x80
 
 section .data
-    msg db "Hello World!", 0x0a ; Die Nachricht
-    len equ $-msg
+  msg db "ASM Tutorial for 32 Bit Intel!", 10, 0
+  len equ $-msg
+
+  buffer: times 100 db '.'
+  quotient: dd 10
 
 section .text
 
-call _entryPoint
-jmp asm_exit
+start:
+  call _entryPoint
+  mov eax, 1000
+  mov ebx, -1024
+  call calculate
+
+  mov eax, 1000
+  mov ebx, 1024
+  call calculate
+
+  call asm_exit
+  ret
+
+asm_exit:
+  push DWORD 0
+  mov eax, SYSCALL_EXIT
+  int SYS_INTERRUPT
+  ret
+
+asm_write:
+  mov eax, SYSCALL_WRITE
+  int SYS_INTERRUPT
+  ret
+
+;;
+;;  CL: NEGATIVE BIT - BOOLEAN
+;; EAX: NUMBER TO CONVERT
+;; EBX: counter
+;;
+integerToString:
+  xor cl, cl
+
+  mov ebx, 0
+  lea si, [buffer]
+  mov di, si
+
+  cmp eax, 0
+  jg loop
+
+  ;; SET NEGATIVE
+  mov cl, 1
+
+  ;; reverse internal negative representation
+  not eax
+  inc eax
+
+  loop:
+    xor edx, edx
+    idiv DWORD [quotient]
+    add edx, 30h
+    mov [si], edx
+    inc si
+    inc ebx
+
+    cmp eax, 0
+    jne loop
+
+  ;; Save current string position
+  push si
+
+  ;; ADD +/- SIGN
+  cmp cl, 0
+  jz positive
+  jg negative
+
+  positive:
+    mov BYTE [si], '+'
+    jmp reverse
+
+  negative:
+    mov BYTE [si], '-'
+    jmp reverse
+
+  ;; Reverse the number in human readable format
+  reverse:
+	;; swap first with last and so on
+    mov al, BYTE [si]
+    mov ah, BYTE [di]
+    mov BYTE [si], ah
+    mov BYTE [di], al
+
+    dec si
+    inc di
+
+    ;; 1 2 3 4 5 6
+    ;; 6         1
+    ;;   5     2
+    ;;     4 3
+    ;;
+    cmp si, di
+    jg reverse
+
+  ;; Restore current String position
+  pop di
+
+  ;; add newline and string terminator
+  newline:
+    mov BYTE [di], 10
+    inc di
+    mov BYTE [di], 0
+    inc di
+
+    ;; add the two chars to the counter
+    add ebx, 2
+
+  ret
+
+calculate:
+  imul ebx
+  call integerToString
+
+  push DWORD ebx
+  push DWORD buffer
+  push DWORD 1
+
+  call asm_write
+
+  add esp, 12
+  ret
 
 _entryPoint:
-    push dword len      ;; Länge des Texts
-    push dword msg      ;; Der Text
-    push dword 1        ;; stdout
-
-	;; call write
-    mov eax, SYSCALL_WRITE
-    sub esp, 4
-    int 0x80
+  push DWORD len      ;; Text Len
+  push DWORD msg      ;; Pointer to String
+  push DWORD 1        ;; stdout
 
-    ;; clean up 3 pushes
-    add esp, 16
-	
-asm_exit:
-	push dword 0
-    mov eax, SYSCALL_EXIT
-	sub esp, 4
-    int 0x80
+  call asm_write      ;;
+
+  add esp, 12         ;; cleanup stack
+  ret
--- a/osx/main.c
+++ b/osx/main.c
@@ -1,10 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-void		entryPoint();
+void entryPoint();
 
-int 
-main(int argc, char **argv)
+int main(int argc, char **argv)
 {
 	printf("Assembler Tutorial reloaded!\n");