Advertisement
Guest User

Prob 8feb2018

a guest
Jan 28th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. a.asm ####################################################################################################################################
  2. bits 32
  3. global start,r
  4. extern exit,printf,maxBytes
  5. import exit msvcrt.dll
  6. import printf msvcrt.dll
  7.  
  8. segment data use32 class=data
  9.     s dd 1234A678h,12345678h,1AC3B47Dh,0FEDC9876h
  10.     lenS equ ($-s)/4
  11.     r times lenS db 0
  12.     d resd 1
  13.     sum resd 1
  14.     pf db "%Xh ",0
  15.     pf2 db `\nThe sum (signed) sum of these bytes is %d\n`,0
  16.  
  17. segment code use32 class=code
  18. start:
  19.     ;maxBytes(s,lenS)
  20.     push lenS
  21.     push s
  22.     call maxBytes
  23.     add ESP,4*2
  24.     ;this will return in eax the sum of the max bytes
  25.     mov [sum],EAX
  26.    
  27.     CLD
  28.     mov ESI,s
  29.     mov EDI,0
  30.     ; now print these bytes using the damn string of byte ranks
  31.     ; that was built using the function from b.asm
  32.     printBytes:
  33.         LODSD ; EAX = ESI , ESI+=4
  34.         mov [d],EAX ; put in [d] the current dword
  35.         mov EBX,d ; put its offset in EBX to use the amazing XLAT
  36.         mov AL,4       ; because of little endian
  37.         sub AL,[r+EDI] ; now we get the good position
  38.         XLAT ; AL := [d+AL]
  39.         movzx EAX,AL
  40.        
  41.         ;now print the byte extracted from the current dword
  42.         push EAX
  43.         push pf
  44.         call [printf]
  45.         add ESP,4*2
  46.        
  47.         inc EDI
  48.         cmp EDI,lenS
  49.         jb printBytes
  50.    
  51.     ;print(pf,sum)
  52.     push dword[sum]
  53.     push pf2
  54.     call [printf]
  55.     add ESP,4*2
  56.    
  57.     jmp $ ; keeps your compiled exe open
  58.     push dword 0
  59.     call [exit]
  60.    
  61.  
  62. b.asm ##################################################################################################################################
  63.  
  64. bits 32
  65. extern r
  66. global maxBytes
  67.  
  68. segment data use32 class=data
  69.     max resb 1 ; used to calculate the max byte
  70.     d resd 1 ; local variable used by the function
  71.  
  72. segment code use32 class=code
  73.     maxBytes:
  74.     mov ESI, [ESP+4] ; s parameter
  75.     mov ECX, [ESP+8] ; lenS parameter
  76.     mov EBX,0 ;initialize sum
  77.     mov EDI,r ;string from a.asm that will store the max bytes ranks
  78.     CLD
  79.    
  80.     parseS:
  81.     push ECX ; save ECX cuz we gonna need it l8r
  82.     LODSD ; put in EAX the current dword
  83.     mov [d],EAX
  84.     mov ECX,0 ; this will be our byte index
  85.     mov AL,1 ; initialize rank
  86.     mov [max],CL ; initialize max byte
  87.     findMax:
  88.         mov DL,[d+ECX]
  89.         cmp DL,[max]
  90.         jbe next
  91.         mov [max],DL
  92.         mov AL,4 ; we have to do this shit bc little endian
  93.         sub AL,CL ; now we have the rank in AL
  94.         next:
  95.         inc ECX
  96.         cmp ECX,4 ; check if we interated over all the 4 bytes of the dword
  97.         jb findMax
  98.    
  99.     STOSB ; in AL we have the rank of the max byte
  100.     movsx EAX,byte[max] ; in [max] we have the max byte
  101.     add EBX,EAX ; add the signed byte to the sum
  102.     pop ECX  ; restore the saved ECX
  103.     loop parseS
  104.    
  105.     mov EAX,EBX ; ret the signed sum in EAX
  106.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement