HasanRasulov

functions.asm

Jan 24th, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SYS_EXIT  equ 1
  2. SYS_READ  equ 3
  3. SYS_WRITE equ 4
  4. STDIN     equ 2
  5. STDOUT    equ 1
  6. LEN       equ 5
  7.  
  8. ;------------------------------------------
  9. ; void iprint(Integer number)
  10. ; Integer printing function (itoa)
  11. iprint:
  12.     push    eax             ; preserve eax on the stack to be restored after function runs
  13.     push    ecx             ; preserve ecx on the stack to be restored after function runs
  14.     push    edx             ; preserve edx on the stack to be restored after function runs
  15.     push    esi             ; preserve esi on the stack to be restored after function runs
  16.     mov     ecx, 0          ; counter of how many bytes we need to print in the end
  17.  
  18. divideLoop:
  19.     inc     ecx             ; count each byte to print - number of characters
  20.     mov     edx, 0          ; empty edx
  21.     mov     esi, 10         ; mov 10 into esi
  22.     idiv    esi             ; divide eax by esi
  23.     add     edx, 48         ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction
  24.     push    edx             ; push edx (string representation of an intger) onto the stack
  25.     cmp     eax, 0          ; can the integer be divided anymore?
  26.     jnz     divideLoop      ; jump if not zero to the label divideLoop
  27.  
  28. printLoop:
  29.     dec     ecx             ; count down each byte that we put on the stack
  30.     mov     eax, esp        ; mov the stack pointer into eax for printing
  31.     call    sprint          ; call our string print function
  32.     pop     eax             ; remove last character from the stack to move esp forward
  33.     cmp     ecx, 0          ; have we printed all bytes we pushed onto the stack?
  34.     jnz     printLoop       ; jump is not zero to the label printLoop
  35.  
  36.     pop     esi             ; restore esi from the value we pushed onto the stack at the start
  37.     pop     edx             ; restore edx from the value we pushed onto the stack at the start
  38.     pop     ecx             ; restore ecx from the value we pushed onto the stack at the start
  39.     pop     eax             ; restore eax from the value we pushed onto the stack at the start
  40.     ret
  41.  
  42.  
  43. ;------------------------------------------
  44. ; void iprintLF(Integer number)
  45. ; Integer printing function with linefeed (itoa)
  46. iprintLF:
  47.     call    iprint          ; call our integer printing function
  48.  
  49.     push    eax             ; push eax onto the stack to preserve it while we use the eax register in this function
  50.     mov     eax, 0Ah        ; move 0Ah into eax - 0Ah is the ascii character for a linefeed
  51.     push    eax             ; push the linefeed onto the stack so we can get the address
  52.     mov     eax, esp        ; move the address of the current stack pointer into eax for sprint
  53.     call    sprint          ; call our sprint function
  54.     pop     eax             ; remove our linefeed character from the stack
  55.     pop     eax             ; restore the original value of eax before our function was called
  56.     ret
  57.  
  58.  
  59. ;------------------------------------------
  60. ; int slen(String message)
  61. ; String length calculation function
  62. slen:
  63.     push    ebx
  64.     mov     ebx, eax
  65.  
  66. nextchar:
  67.     cmp     byte [eax], 0
  68.     jz      finished
  69.     inc     eax
  70.     jmp     nextchar
  71.  
  72. finished:
  73.     sub     eax, ebx
  74.     pop     ebx
  75.     ret
  76.  
  77.  
  78. ;------------------------------------------
  79. ; void sprint(String message)
  80. ; String printing function
  81. sprint:
  82.     push    edx
  83.     push    ecx
  84.     push    ebx
  85.     push    eax
  86.     call    slen
  87.  
  88.     mov     edx, eax
  89.     pop     eax
  90.  
  91.     mov     ecx, eax
  92.     mov     ebx, 1
  93.     mov     eax, 4
  94.     int     80h
  95.  
  96.     pop     ebx
  97.     pop     ecx
  98.     pop     edx
  99.     ret
  100.  
  101.  
  102. ;------------------------------------------
  103. ; void sprintLF(String message)
  104. ; String printing with line feed function
  105. sprintLF:
  106.     call    sprint
  107.  
  108.     push    eax
  109.     mov     eax, 0AH
  110.     push    eax
  111.     mov     eax, esp
  112.     call    sprint
  113.     pop     eax
  114.     pop     eax
  115.     ret
  116.  
  117.  
  118. ;------------------------------------------
  119. ; void exit()
  120. ; Exit program and restore resources
  121. quit:
  122.     mov     ebx, 0
  123.     mov     eax, 1
  124.     int     80h
  125.     ret
  126.  
  127. ;------------------------------------------
  128. ; int atoi(Integer number)
  129. ; Ascii to integer function (atoi)
  130. atoi:
  131.     push    ebx             ; preserve ebx on the stack to be restored after function runs
  132.     push    ecx             ; preserve ecx on the stack to be restored after function runs
  133.     push    edx             ; preserve edx on the stack to be restored after function runs
  134.     push    esi             ; preserve esi on the stack to be restored after function runs
  135.     mov     esi, eax        ; move pointer in eax into esi (our number to convert)
  136.     mov     eax, 0          ; initialise eax with decimal value 0
  137.     mov     ecx, 0          ; initialise ecx with decimal value 0
  138.  
  139. .multiplyLoop:
  140.     xor     ebx, ebx        ; resets both lower and uppper bytes of ebx to be 0
  141.     mov     bl, [esi+ecx]   ; move a single byte into ebx register's lower half
  142.     cmp     bl, 48          ; compare ebx register's lower half value against ascii value 48 (char value 0)
  143.     jl      .finished       ; jump if less than to label finished
  144.     cmp     bl, 57          ; compare ebx register's lower half value against ascii value 57 (char value 9)
  145.     jg      .finished       ; jump if greater than to label finished
  146.  
  147.     sub     bl, 48          ; convert ebx register's lower half to decimal representation of ascii value
  148.     add     eax, ebx        ; add ebx to our interger value in eax
  149.     mov     ebx, 10         ; move decimal value 10 into ebx
  150.     mul     ebx             ; multiply eax by ebx to get place value
  151.     inc     ecx             ; increment ecx (our counter register)
  152.     jmp     .multiplyLoop   ; continue multiply loop
  153.  
  154. .finished:
  155.     cmp     ecx, 0          ; compare ecx register's value against decimal 0 (our counter register)
  156.     je      .restore        ; jump if equal to 0 (no integer arguments were passed to atoi)
  157.     mov     ebx, 10         ; move decimal value 10 into ebx
  158.     div     ebx             ; divide eax by value in ebx (in this case 10)
  159.  
  160. .restore:
  161.     pop     esi             ; restore esi from the value we pushed onto the stack at the start
  162.     pop     edx             ; restore edx from the value we pushed onto the stack at the start
  163.     pop     ecx             ; restore ecx from the value we pushed onto the stack at the start
  164.     pop     ebx             ; restore ebx from the value we pushed onto the stack at the start
  165.     ret
  166.  
  167.  
  168.  
Add Comment
Please, Sign In to add comment