Advertisement
Guest User

Untitled

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