osx/hello.asm
author Markus Bröker<broeker.markus@googlemail.com>
Sun, 10 Feb 2019 13:17:01 +0100
changeset 173 374a86886bc5
parent 172 43ae72f88d06
permissions -rw-r--r--
LAST-DIGIT-BUG: INCREMENT before LF
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
     1
;;
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
     2
;; hello.asm
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
     3
;; Basics in Assembler
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
     4
;;
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
     5
global start
171
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
     6
global _entryPoint
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
     7
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
     8
%define  SYSCALL_EXIT 0x1
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
     9
%define SYSCALL_WRITE 0x4
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    10
%define SYS_INTERRUPT 0x80
171
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
    11
168
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
    12
section .data
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    13
  msg db "ASM Tutorial for 32 Bit Intel!", 10, 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    14
  len equ $-msg
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    15
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    16
  buffer: times 100 db '.'
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    17
  quotient: dd 10
168
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
    18
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
    19
section .text
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
    20
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    21
start:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    22
  call _entryPoint
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    23
  mov eax, 1000
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    24
  mov ebx, -1024
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    25
  call calculate
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    26
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    27
  mov eax, 1000
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    28
  mov ebx, 1024
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    29
  call calculate
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    30
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    31
  call asm_exit
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    32
  ret
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    33
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    34
asm_exit:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    35
  push DWORD 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    36
  mov eax, SYSCALL_EXIT
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    37
  int SYS_INTERRUPT
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    38
  ret
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    39
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    40
asm_write:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    41
  mov eax, SYSCALL_WRITE
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    42
  int SYS_INTERRUPT
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    43
  ret
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    44
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    45
;;
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    46
;;  CL: NEGATIVE BIT - BOOLEAN
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    47
;; EAX: NUMBER TO CONVERT
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    48
;; EBX: counter
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    49
;;
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    50
integerToString:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    51
  xor cl, cl
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    52
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    53
  mov ebx, 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    54
  lea si, [buffer]
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    55
  mov di, si
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    56
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    57
  cmp eax, 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    58
  jg loop
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    59
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    60
  ;; SET NEGATIVE
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    61
  mov cl, 1
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    62
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    63
  ;; reverse internal negative representation
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    64
  not eax
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    65
  inc eax
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    66
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    67
  loop:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    68
    xor edx, edx
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    69
    idiv DWORD [quotient]
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    70
    add edx, 30h
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    71
    mov [si], edx
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    72
    inc si
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    73
    inc ebx
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    74
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    75
    cmp eax, 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    76
    jne loop
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    77
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    78
  ;; Save current string position
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    79
  push si
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    80
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    81
  ;; ADD +/- SIGN
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    82
  cmp cl, 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    83
  jz positive
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    84
  jg negative
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    85
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    86
  positive:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    87
    mov BYTE [si], '+'
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    88
    jmp reverse
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    89
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    90
  negative:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    91
    mov BYTE [si], '-'
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    92
    jmp reverse
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    93
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    94
  ;; Reverse the number in human readable format
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    95
  reverse:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    96
	;; swap first with last and so on
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    97
    mov al, BYTE [si]
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    98
    mov ah, BYTE [di]
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
    99
    mov BYTE [si], ah
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   100
    mov BYTE [di], al
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   101
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   102
    dec si
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   103
    inc di
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   104
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   105
    ;; 1 2 3 4 5 6
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   106
    ;; 6         1
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   107
    ;;   5     2
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   108
    ;;     4 3
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   109
    ;;
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   110
    cmp si, di
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   111
    jg reverse
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   112
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   113
  ;; Restore current String position
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   114
  pop di
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   115
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   116
  ;; add newline and string terminator
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   117
  newline:
173
374a86886bc5 LAST-DIGIT-BUG: INCREMENT before LF
Markus Bröker<broeker.markus@googlemail.com>
parents: 172
diff changeset
   118
    inc di
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   119
    mov BYTE [di], 10
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   120
    inc di
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   121
    mov BYTE [di], 0
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   122
    inc di
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   123
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   124
    ;; add the two chars to the counter
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   125
    add ebx, 2
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   126
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   127
  ret
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   128
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   129
calculate:
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   130
  imul ebx
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   131
  call integerToString
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   132
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   133
  push DWORD ebx
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   134
  push DWORD buffer
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   135
  push DWORD 1
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   136
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   137
  call asm_write
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   138
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   139
  add esp, 12
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   140
  ret
168
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
   141
171
c6e0af68825a Entrypoint and RET fixed
Markus Bröker<broeker.markus@googlemail.com>
parents: 168
diff changeset
   142
_entryPoint:
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   143
  push DWORD len      ;; Text Len
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   144
  push DWORD msg      ;; Pointer to String
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   145
  push DWORD 1        ;; stdout
168
dfb60716880c Broken OSX Demo added
Markus Bröker<broeker.markus@googlemail.com>
parents:
diff changeset
   146
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   147
  call asm_write      ;;
173
374a86886bc5 LAST-DIGIT-BUG: INCREMENT before LF
Markus Bröker<broeker.markus@googlemail.com>
parents: 172
diff changeset
   148
  
172
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   149
  add esp, 12         ;; cleanup stack
43ae72f88d06 2er Komplement und integerToString
Markus Bröker<broeker.markus@googlemail.com>
parents: 171
diff changeset
   150
  ret