Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 5.67 KB | None | 0 0
  1. ; assn10 - Recursive List
  2. ; CS340
  3. ; Nicholas Hershy
  4.  
  5. include Irvine32.inc
  6. includelib Irvine32.lib
  7.  
  8. .386
  9. ;.model flat,stdcall
  10. .stack 4096
  11. ExitProcess proto,dwExitCode:dword
  12.  
  13. ; '\r' comes before '\n' in txt file
  14. CR EQU 0Dh
  15. LF EQU 0Ah
  16. NULL EQU 00h
  17. COM EQU 2Ch ;comma
  18. SPC EQU 20h
  19. PER EQU 2Eh ;period/dot
  20.  
  21. .data
  22.  
  23.     ofilename BYTE "C:\Users\Nicholas Hershy\Desktop\asm_output.txt",0
  24.     ifilename BYTE "C:\Users\Nicholas Hershy\Desktop\emply.dat",0
  25.     err BYTE "Error: Unable to open input file",LF,NULL
  26.     good BYTE "Input file opened successfully",LF,NULL
  27.     fileHandle DWORD ? ;this is the file handle
  28.     ifileHandle DWORD ?
  29.  
  30.     BUFFER_SIZE = 600
  31.     buffer BYTE BUFFER_SIZE DUP (NULL)
  32.     bytesRead BYTE ?
  33.     temp DWORD ?
  34.  
  35.     Num1 DWORD 12
  36.     Num2 DWORD 7
  37.     Prod DWORD 0
  38.  
  39.     printNewLine BYTE LF,NULL
  40.  
  41.     ProductAry BYTE 5 dup (' ')
  42.  
  43.     numAry BYTE '1','2','3','4','5','6','7','8','9',NULL
  44.     printNum BYTE 1 dup (' '),NULL
  45.  
  46.  
  47. .code
  48. main proc
  49.  
  50.     ;opens file and overwrite each time
  51.     ;-------------------------------------------
  52.     ;open outfile here - p.468 reference
  53.     MOV edx, OFFSET ofilename
  54.     call CreateOutputFile
  55.     MOV fileHandle, eax
  56.     ;-------------------------------------------
  57.  
  58.     ;opens inputfile p.163 reference
  59.     ;---------------------------------------------------------
  60.     LEA edx,ifilename
  61.     CALL OpenInputFile
  62.     .IF eax == INVALID_HANDLE_VALUE
  63.         LEA esi,err
  64.         CALL PrintToFile
  65.     .ELSE
  66.         MOV ifileHandle,eax
  67.         LEA esi,good
  68.         CALL PrintToFile
  69.     .ENDIF
  70.     ;----------------------------------------------------------
  71.  
  72.     call WriteWindowsMsg ;debug
  73.  
  74.     ;reads from input file and puts into buffer ary
  75.     ;also finds bytesRead
  76.     ;--------------------------------------------------------
  77.     MOV edx,OFFSET buffer ; points to buffer
  78.     MOV ecx,BUFFER_SIZE ; max bytes to read
  79.     MOV eax,ifilehandle
  80.     CALL ReadFromFile ; read the file
  81.     MOV bytesRead,al
  82.     ;---------------------------------------------------------
  83.  
  84.     PUSH OFFSET numAry
  85.     CALL RecursivePrint
  86.  
  87.     LEA esi,printNewLine
  88.     CALL PrintToFile
  89.  
  90.     PUSH OFFSET numAry
  91.     CALL RecursivePrintRev
  92.  
  93.     invoke ExitProcess,0
  94. main endp
  95.  
  96.  
  97. ;------------------------- itoa ------------------------------
  98. ;converts int to ascii
  99. ;condtion1: byte array must be blanked out first (Blankout proc)
  100. ;condition2: starts from end of the ary and moves backward...
  101. ;   point to one location after the end of ary as starting pos.
  102. ;param1: takes int (dword) variable into eax
  103. ;param2: takes 'BYTE x dup (?)' char array into esi
  104. ;--------------------------------------------------------------
  105.  
  106. itoa proc
  107.     push ecx
  108.     nextdigit: MOV edx,0
  109.     MOV ebx, 10
  110.     DIV ebx     ;divide eax by 10 chops off last digit?
  111.     ADD dl, 30h     ;30h is zero?
  112.     DEC esi
  113.     MOV [esi], dl
  114.     CMP eax, 0
  115.     JNE nextdigit
  116.     pop ecx
  117.     ret
  118.  
  119. itoa endp
  120.  
  121. ;---------------------------------atoi-----------------------------
  122. ;converts ary of ascii to int
  123. ;param1: takes byte array into esi
  124. ;returns result in eax
  125. ;condition: stops when does not find an ascii digit 0-9
  126. ;----------------------------------------------------------------
  127.  
  128. atoi proc
  129.  
  130.     MOV temp,10
  131.     MOV ebx,0
  132.     MOV eax,0
  133.     nextDigit: MOV bl,[esi]
  134.     CMP bl, '0'
  135.     JL notADigit
  136.     CMP bl,'9'
  137.     JG notADigit
  138.     ADD bl,-30h
  139.     IMUL temp
  140.     ADD eax, ebx
  141.     INC esi
  142.     JMP nextDigit
  143.     notADigit: ret
  144.  
  145. atoi endp
  146.  
  147. ;---------------------------PrintToFile-----------------------------
  148. ;prints ascii text to output file until NULL is hit
  149. ;param1: takes byte array into esi
  150. ;condition: must already have var called outfile open and set
  151. ;   to valid file handle
  152. ;------------------------------------------------------------------
  153.  
  154. PrintToFile proc
  155.  
  156.     push ecx
  157.     nextchar: MOV dl, [esi]
  158.     CMP dl, NULL
  159.     JE exitloop
  160.     MOV edx, esi
  161.     MOV ecx,1
  162.     MOV eax,fileHandle
  163.     CALL WriteToFile
  164.     INC esi
  165.     JNE nextchar
  166.     exitloop:
  167.     pop ecx
  168.     ret
  169.  
  170. PrintToFile endp
  171.  
  172. ;------------------------- Blankout ---------------------------
  173. ;blanks out an array with spaces
  174. ;condition: must be a 'BYTE x dup (x)' of size x
  175. ;param1: takes 'BYTE x dup (x)' char array into esi
  176. ;--------------------------------------------------------------
  177.  
  178. Blankout proc
  179.  
  180.     PUSH ecx
  181.     MOV ecx,0
  182.     next: MOV dl, 20h
  183.     MOV [esi], dl
  184.     INC esi
  185.     INC ecx
  186.     cmp ecx,1 ;size of ary
  187.     JNE next
  188.     POP ecx
  189.     ret
  190.  
  191. Blankout endp
  192.  
  193. ;--------------------------RecursivePrint-------------------------
  194. ;prints out an array in order using recursion
  195. ;param1: beginning address of the array passed in on the stack
  196. ;----------------------------------------------------------------
  197.  
  198. RecursivePrint proc
  199.  
  200.     A EQU 8[ebp]
  201.     MOV eax,0
  202.  
  203.     ;necessary
  204.     PUSH ebp
  205.     MOV ebp,esp
  206.    
  207.     MOV esi,A
  208.     MOV edi,esi ;uses this to increment elements later
  209.     MOV al,[esi]
  210.     ;if (ary[x] = NULL)
  211.     CMP al,NULL
  212.     JE BC
  213.  
  214.     ;else print ary element
  215.     LEA esi,printNum
  216.     MOV [esi],al
  217.     CALL PrintToFile
  218.  
  219.     ;move to the next element in the ary
  220.     INC edi
  221.     PUSH edi
  222.     CALL RecursivePrint
  223.  
  224.     BC:
  225.     MOV esp,ebp
  226.     POP ebp
  227.     ret ; don't think I need to return a num bc this is void function
  228.  
  229. RecursivePrint endp
  230.  
  231. ;--------------------------RecursivePrintRev---------------------
  232. ;prints out an ary in reverse order using recursion
  233. ;param1: beginning element of ary passed in on the stack
  234. ;----------------------------------------------------------------
  235.  
  236. RecursivePrintRev proc
  237.  
  238.     MOV eax,0
  239.  
  240.     ;necessary
  241.     PUSH ebp
  242.     MOV ebp,esp
  243.    
  244.     MOV esi,A
  245.     MOV edi,esi ;uses this to increment elements later
  246.     MOV al,[esi]
  247.     ;if (ary[x] = NULL)
  248.     CMP al,NULL
  249.     JE BC
  250.  
  251.     ;move to the next element in the ary
  252.     INC edi
  253.     PUSH edi
  254.     CALL RecursivePrintRev
  255.  
  256.     ;else print ary element
  257.     LEA esi,printNum
  258.     MOV al,A
  259.     MOV [esi],al
  260.     CALL PrintToFile
  261.  
  262.     BC:
  263.     MOV esp,ebp
  264.     POP ebp
  265.     ret ; don't think I need to return a num bc this is void function
  266.  
  267. RecursivePrintRev endp
  268.  
  269. end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement