section .text
global main
main:
mov esi, 10 ; ESI holds current number
mov eax, 0 ; EAX holds sum of numbers
push 10 ; Line feed for end of line (Stack is first in, last out -> Line feed will be last char)
mov edi, 10 ; Divisor of 10 for seperating digits of number (used in divisionLoop)
; Sum numbers 1 through 10
sumLoop:
add eax, esi ; Add number to sum
dec esi ; Next number
jnz sumLoop ; Loop until 0
; Seperate eax into its digits, by dividing by 10 multiple times,
; where in each division the remainder will be a single digit
; and the quotient will be the remaining digits used as dividend in next loop run
divisionLoop:
mov edx, 0 ; Make sure edx is empty, as it is used as upper half of dividend
div edi ; Divide eax by edi (= 10) => quotient is in eax (= Rest of digits for next loop), remainder in edx (= Single digit)
add edx, 48 ; Make edx (digit) a char representing its value by adding '0' to it
push edx ; Push char to stack for usage in print later
cmp eax, 0 ; Loop until quotient is 0 (=> no more digits left)
jne divisionLoop
; Print digits from Stack one by one
printLoop:
mov eax, 4
mov ebx, 1
mov ecx, esp ; Print top of stack (esp always points to top of stack)
mov edx, 1 ; Length of 1 byte (= 1 char)
int 80h
pop esi ; Remove top of stack
cmp esi, 10 ; Loop until Line feed is reached
jne printLoop
exit:
mov eax, 1
mov ebx, 0 ; Exit code 0
int 80h
Hallo,
hier steht ein Programm in Assembler, dass die Zahlen von 1 bis 10 addiert.
Die 55 durch 10 teilt und die Reste der Division dann in einem Stack speichert.
push....
Am Ende wird alles über den Standardkanal ausgegeben auf der Konsole.
Wenn ich 55 / 10 teile ergibt das Quotient 5 steht in Register eax und der Rest hier auch 5 in edx Register. Bevor ich jetzt den Rest auf den Stack lege wird die Zahl mit 48 addiert. ergbit 53 ist das char Zeichen 5 im ASCII Code.
Heißt das jetzt, dass die Ergebnisse bei einer ganzzahligen Division im Assembler-Code immer in ASCII-Zeichen gespeichert sind?
Achsoo vielen Dank!
Also ist die ASCII Tabelle auch ein unsigned char?