Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %include 'functions.asm'
- section .data ;Uninitialized data
- num_1 db '123' ,0h
- num_2 db '2' ,0h
- section .bss
- num1 resb LEN
- num2 resb LEN
- section .text
- 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,3
- ;mov ebx,2
- ;mov ecx,num1
- ;mov edx,LEN
- ;int 80h
- ;mov byte [num1+LEN-1], 0h
- mov eax,num_1
- call reverse ;reverse first number
- push eax ;push result to stack
- call iprintLF
- ;mov eax,3
- ;mov ebx,2
- ;mov ecx,num2
- ;mov edx,LEN
- ;int 80h
- ;mov byte [num2+LEN-1], 0h
- mov eax,num_2 ;move second number to eax
- call reverse ;reverse second number
- call iprintLF
- pop ebx ;pop first reversed number
- add eax,ebx ;add them up
- call iprintLF
- ; below code section is for reverse the sum
- ; we can't use reverse function beacuse it accepts string but we have decimal value
- mov ebx, 10 ;move 10 to ebx as divident to get remainder
- xor ecx, ecx ;clear ecx for
- godivide:
- xor edx, edx ; clear remainder
- div ebx ; divide eax by 10
- cmp edx , 0
- je godivide ;skip the zero
- add edx, '0' ;convert digit to ascii char
- push eax ;reserve
- push ebx ;reserve
- push edx ;reserve
- mov ecx,esp ;get the adress of first digit from stack(remainder edx)
- mov edx,1
- mov ebx,STDOUT
- mov eax,SYS_WRITE
- int 80h
- pop edx ;restore
- pop ebx ;restore
- pop eax ;restore
- cmp eax, 0 ; if eax
- jne godivide ; is not equal to zero, divide again
- call quit
Add Comment
Please, Sign In to add comment