Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .text:
  2.   global _start
  3.        
  4. _start:
  5.   ;;; main() { }
  6.  
  7. hex2ascii:
  8.   ; stack: [ arg | return address | saved ebp ]
  9.   ;           ^ ebp + 8  ^ ebp + 4    ^ ebp
  10.   mov     eax, [ebp+8]    ; fetch argument from stack
  11.   and     eax, 0x0F
  12.   cmp     eax, 0xA
  13.   jge     AthruF
  14.   add     eax, '0'
  15.   jmp     done
  16. AthruF:
  17.   sub     eax, 0xA
  18.   add     eax, 'A'          
  19. done:
  20.   ret                     ; return result in eax
  21.  
  22. printShort:
  23.   ; prelude
  24.   push  ebp
  25.   mov   ebp, esp
  26.  
  27.   sub   esp, 8            ; make space for char ascii[] with word alignment
  28.   mov   esi, ebp          ; set string address
  29.   sub   esi, 8            ; esi = ebp - 8
  30.   mov   [esi+4], byte '\n'; ascii[4] = '\n'
  31.  
  32.   ; stack: [ arg | return address | saved ebp | 3 padding bytes | ascii[] ]
  33.   ;                                   ^ ebp
  34.   mov   edx, [ebp+8]      ; load argument into edx
  35.  
  36.   and   edx, 0x0F         ; data & 0x0F
  37.   push  edx               ; push for call
  38.   call  hex2ascii         ; eax = hex2ascii(data & 0x0f)
  39.   pop   edx               ; clean up stack
  40.   mov   [esi+3], al       ; ascii[3] = (byte) eax
  41.   shr   edx, 4            ; data >>= 4  
  42.  
  43.   and   edx, 0x0F         ; data & 0x0F
  44.   push  edx               ; push for call
  45.   call  hex2ascii         ; eax = hex2ascii(data & 0x0f)
  46.   pop   edx               ; clean up stack
  47.   mov   [esi+2], al       ; ascii[2] = (byte) eax
  48.   shr   edx, 4            ; data >>= 4  
  49.  
  50.   and   edx, 0x0F         ; data & 0x0F
  51.   push  edx               ; push for call
  52.   call  hex2ascii         ; eax = hex2ascii(data & 0x0f)
  53.   pop   edx               ; clean up stack
  54.   mov   [esi+1], al       ; ascii[1] = (byte) eax
  55.   shr   edx, 4            ; data >>= 4  
  56.  
  57.   and   edx, 0x0F         ; data & 0x0F
  58.   push  edx               ; push for call
  59.   call  hex2ascii         ; eax = hex2ascii(data & 0x0f)
  60.   pop   edx               ; clean up stack
  61.   mov   [esi], al         ; ascii[0] = (byte) eax
  62.  
  63.   mov   eax, 4            ; syscall - write()
  64.   mov   ebx, 1            ; set stdout
  65.   mov   ecx, esi          ; set string address
  66.   mov   edx, 5            ; print 5 bytes
  67.   int   0x80
  68.  
  69.   leave
  70.   ret
  71.  
  72. exit:
  73.   mov     eax, 1
  74.   int     0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement