Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .bss
- num resb 10
- section .data
- section .text
- atoi:
- push ebx ; preserve ebx on the stack to be restored after function runs
- push ecx ; preserve ecx on the stack to be restored after function runs
- push edx ; preserve edx on the stack to be restored after function runs
- push esi ; preserve esi on the stack to be restored after function runs
- mov esi, eax ; move pointer in eax into esi (our number to convert)
- mov eax, 0 ; initialise eax with decimal value 0
- mov ecx, 0 ; initialise ecx with decimal value 0
- .multiplyLoop:
- xor ebx, ebx ; resets both lower and uppper bytes of ebx to be 0
- mov bl, [esi+ecx] ; move a single byte into ebx register's lower half
- cmp bl, 48 ; compare ebx register's lower half value against ascii value 48 (char value 0)
- jl .finished ; jump if less than to label finished
- cmp bl, 57 ; compare ebx register's lower half value against ascii value 57 (char value 9)
- jg .finished ; jump if greater than to label finished
- sub bl, 48 ; convert ebx register's lower half to decimal representation of ascii value
- add eax, ebx ; add ebx to our interger value in eax
- mov ebx, 10 ; move decimal value 10 into ebx
- mul ebx ; multiply eax by ebx to get place value
- inc ecx ; increment ecx (our counter register)
- jmp .multiplyLoop ; continue multiply loop
- .finished:
- cmp ecx, 0 ; compare ecx register's value against decimal 0 (our counter register)
- je .restore ; jump if equal to 0 (no integer arguments were passed to atoi)
- mov ebx, 10 ; move decimal value 10 into ebx
- div ebx ; divide eax by value in ebx (in this case 10)
- .restore:
- pop esi ; restore esi from the value we pushed onto the stack at the start
- pop edx ; restore edx from the value we pushed onto the stack at the start
- pop ecx ; restore ecx from the value we pushed onto the stack at the start
- pop ebx ; restore ebx from the value we pushed onto the stack at the start
- ret
- global _start
- _start:
- mov eax,3
- mov ebx,0
- mov ecx,num
- mov edx,10
- int 80h
- mov ebx,1 ;divident
- mov ecx,0 ;number of zeros
- mov edx,5 ;to multiply the divident
- mov eax,0 ;quotient of divison
- .loop:
- add ecx,eax ;add the quotient of result
- mov eax,ebx ;move divident to eax
- mov edx,5 ;move 5 to edx to multiply
- mul edx ;multiply last divident with 5
- mov ebx,eax ;move divident to ebx
- mov eax,num ;move number to eax to call atoi
- call atoi
- div ebx ;divide number by current divident
- cmp eax,1 ;if quotient is less than 1 finish
- jge .loop
- out:
- mov eax,ecx ;move result to eax to print
- ;iprint label will print the integer number in eax to stdout
- .iprint:
- mov ecx, 0 ; counter of how many bytes we need to print in the end
- mov esi, 10 ; mov 10 into esi
- .divideLoop:
- inc ecx ; count each byte to print - number of characters
- mov edx, 0 ; empty edx
- idiv esi ; divide eax by esi
- add edx, 48 ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction
- push edx ; push edx (string representation of an intger) onto the stack
- cmp eax, 0 ; can the integer be divided anymore?
- jnz .divideLoop ; jump if not zero to the label divideLoop
- .printLoop:
- dec ecx ; count down each byte that we put on the stack
- mov esi,ecx ;store number of digits is esi to move stack pointer there
- mov ecx, esp ; mov the stack pointer into eax for printing
- mov edx, 1
- mov eax, 4
- mov ebx, 1
- int 80h
- mov ecx,esi ;get back number of digits
- pop eax ; remove last character from the stack to move esp forward
- cmp ecx, 0 ; have we printed all bytes we pushed onto the stack?
- jnz .printLoop ; jump is not zero to the label printLoop
- .exit:
- mov ebx, 0
- mov eax, 1
- int 80h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement