Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %include 'functions.asm'
- section .data
- section .bss
- input resb LEN
- num resb LEN+1 ;store result of itoa function
- section .text
- itoa:
- 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
- mov ebx,10 ; store 10 to divide the number to get reminder
- xor ecx,ecx ; clear and store number of digits
- godivide:
- xor edx, edx ; clear remainder
- div ebx ; divide eax by 10
- push edx ; put edx on stack
- inc ecx ; increment number of digits
- cmp eax, 0 ; if eax
- jne godivide ; is not equal to zero, divide again
- mov ebx, 0
- nextdigit:
- pop eax ;pop of digit
- add eax, 30h ;add '0' to eax
- mov [num+ebx],eax ;move current digit to buffer
- inc ebx ;increment index
- dec ecx ;move down through digits
- cmp ecx, 0 ;if ecx
- jg nextdigit ;is greater than 0, jump nextdigit
- ;mov byte [num+LEN],0h
- mov eax,num ;move final result to eax
- 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
- reverse:
- 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
- mov esi, eax ; move pointer in eax into esi (our number to convert)
- call slen ; initialise eax with decimal value 0
- mov ecx, eax
- dec ecx ; initialise ecx with decimal value 0
- mov eax, 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
- 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
- dec ecx
- cmp ecx, -1 ; compare ebx register's lower half value against ascii value 48 (char value 0)
- je .finished ; jump if less than to label finished
- 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,SYS_READ
- mov ebx,STDIN
- mov ecx,input ;read input
- mov edx,LEN
- int 80h
- ;mov byte [input+LEN-1], 0h
- mov eax,input
- call atoi ;convert string to decimal
- mov edx,eax ;move result to edx
- mov eax,input ;prepare arg for reverse
- loop:
- call reverse ;reverse string number
- cmp eax,edx ;compare decimal reversed with orginal
- je print ;if equal jump to print
- inc edx ;otherwise,incerement original
- mov eax,edx ;move to eax and convert it to string for reverse function
- call itoa
- jmp loop ;jump uncontionally
- print:
- mov eax,edx
- call iprintLF ;print decimal result
- call quit ;exit program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement