Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
coding:nasm [2025/04/28 18:42] admin [Windows] |
coding:nasm [2025/04/28 18:47] (aktuell) admin [Calculator] |
||
---|---|---|---|
Zeile 156: | Zeile 156: | ||
=====Windows===== | =====Windows===== | ||
- | < | + | ====Input/ |
+ | |||
+ | < | ||
;;Assemble and link with | ;;Assemble and link with | ||
;nasm -fwin32 hello.asm | ;nasm -fwin32 hello.asm | ||
Zeile 167: | Zeile 169: | ||
segment .data | segment .data | ||
- | msg: db "Enter your ID", 0xA, 0xD, 0 ; note the null terminator. | + | msg: db "String eingeben", 0xA, 0xD, 0 ; null terminator. |
formatin: db " | formatin: db " | ||
Zeile 192: | Zeile 194: | ||
| | ||
+ | </ | ||
+ | |||
+ | ====Calculator==== | ||
+ | |||
+ | <code asm> | ||
+ | ; Assemble and link with: | ||
+ | ; nasm -f win32 calc.asm | ||
+ | ; gcc -o calc calc.obj | ||
+ | |||
+ | global _main | ||
+ | extern _scanf | ||
+ | extern _printf | ||
+ | |||
+ | segment .data | ||
+ | msg1 db "Enter first number: ", 0xA, 0 | ||
+ | msg2 db "Enter second number: ", 0xA, 0 | ||
+ | msg3 db "Enter operation (+, -, *, /): ", 0xA, 0 | ||
+ | formatint db " | ||
+ | formatchar db " %c", 0 ; Leerzeichen für scanf, damit Whitespaces ignoriert werden | ||
+ | formatout db " | ||
+ | |||
+ | segment .bss | ||
+ | num1 resd 1 | ||
+ | num2 resd 1 | ||
+ | result resd 1 | ||
+ | operation resb 1 | ||
+ | |||
+ | segment .text | ||
+ | |||
+ | _main: | ||
+ | |||
+ | ; Eingabe erste Zahl | ||
+ | push msg1 | ||
+ | call _printf | ||
+ | add esp, 4 | ||
+ | |||
+ | push num1 | ||
+ | push formatint | ||
+ | call _scanf | ||
+ | add esp, 8 | ||
+ | |||
+ | ; Eingabe zweite Zahl | ||
+ | push msg2 | ||
+ | call _printf | ||
+ | add esp, 4 | ||
+ | |||
+ | push num2 | ||
+ | push formatint | ||
+ | call _scanf | ||
+ | add esp, 8 | ||
+ | |||
+ | ; Eingabe Rechenoperation | ||
+ | push msg3 | ||
+ | call _printf | ||
+ | add esp, 4 | ||
+ | |||
+ | push operation | ||
+ | push formatchar | ||
+ | call _scanf | ||
+ | add esp, 8 | ||
+ | |||
+ | ; Rechnen | ||
+ | mov eax, [num1] | ||
+ | mov ebx, [num2] | ||
+ | |||
+ | movzx ecx, byte [operation] | ||
+ | |||
+ | cmp ecx, ' | ||
+ | je addieren | ||
+ | |||
+ | cmp ecx, ' | ||
+ | je subtrahieren | ||
+ | |||
+ | cmp ecx, ' | ||
+ | je multiplizieren | ||
+ | |||
+ | cmp ecx, '/' | ||
+ | je dividieren | ||
+ | |||
+ | ; Unbekannte Operation -> abbrechen | ||
+ | jmp ende | ||
+ | |||
+ | addieren: | ||
+ | add eax, ebx | ||
+ | jmp speichern | ||
+ | |||
+ | subtrahieren: | ||
+ | sub eax, ebx | ||
+ | jmp speichern | ||
+ | |||
+ | multiplizieren: | ||
+ | imul eax, ebx | ||
+ | jmp speichern | ||
+ | |||
+ | dividieren: | ||
+ | cmp ebx, 0 | ||
+ | je ende ; Division durch Null verhindern | ||
+ | cdq ; Vorzeichen erweitern für idiv | ||
+ | idiv ebx | ||
+ | jmp speichern | ||
+ | |||
+ | speichern: | ||
+ | mov [result], eax | ||
+ | |||
+ | ; Ausgabe | ||
+ | push dword [result] | ||
+ | push formatout | ||
+ | call _printf | ||
+ | add esp, 8 | ||
+ | |||
+ | ende: | ||
+ | ret | ||
</ | </ | ||