Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; assn10 - Recursive List
- ; CS340
- ; Nicholas Hershy
- include Irvine32.inc
- includelib Irvine32.lib
- .386
- ;.model flat,stdcall
- .stack 4096
- ExitProcess proto,dwExitCode:dword
- ; '\r' comes before '\n' in txt file
- CR EQU 0Dh
- LF EQU 0Ah
- NULL EQU 00h
- COM EQU 2Ch ;comma
- SPC EQU 20h
- PER EQU 2Eh ;period/dot
- .data
- ofilename BYTE "C:\Users\Nicholas Hershy\Desktop\asm_output.txt",0
- ifilename BYTE "C:\Users\Nicholas Hershy\Desktop\emply.dat",0
- err BYTE "Error: Unable to open input file",LF,NULL
- good BYTE "Input file opened successfully",LF,NULL
- fileHandle DWORD ? ;this is the file handle
- ifileHandle DWORD ?
- BUFFER_SIZE = 600
- buffer BYTE BUFFER_SIZE DUP (NULL)
- bytesRead BYTE ?
- temp DWORD ?
- Num1 DWORD 12
- Num2 DWORD 7
- Prod DWORD 0
- printNewLine BYTE LF,NULL
- ProductAry BYTE 5 dup (' ')
- numAry BYTE '1','2','3','4','5','6','7','8','9',NULL
- printNum BYTE 1 dup (' '),NULL
- .code
- main proc
- ;opens file and overwrite each time
- ;-------------------------------------------
- ;open outfile here - p.468 reference
- MOV edx, OFFSET ofilename
- call CreateOutputFile
- MOV fileHandle, eax
- ;-------------------------------------------
- ;opens inputfile p.163 reference
- ;---------------------------------------------------------
- LEA edx,ifilename
- CALL OpenInputFile
- .IF eax == INVALID_HANDLE_VALUE
- LEA esi,err
- CALL PrintToFile
- .ELSE
- MOV ifileHandle,eax
- LEA esi,good
- CALL PrintToFile
- .ENDIF
- ;----------------------------------------------------------
- call WriteWindowsMsg ;debug
- ;reads from input file and puts into buffer ary
- ;also finds bytesRead
- ;--------------------------------------------------------
- MOV edx,OFFSET buffer ; points to buffer
- MOV ecx,BUFFER_SIZE ; max bytes to read
- MOV eax,ifilehandle
- CALL ReadFromFile ; read the file
- MOV bytesRead,al
- ;---------------------------------------------------------
- PUSH OFFSET numAry
- CALL RecursivePrint
- LEA esi,printNewLine
- CALL PrintToFile
- PUSH OFFSET numAry
- CALL RecursivePrintRev
- invoke ExitProcess,0
- main endp
- ;------------------------- itoa ------------------------------
- ;converts int to ascii
- ;condtion1: byte array must be blanked out first (Blankout proc)
- ;condition2: starts from end of the ary and moves backward...
- ; point to one location after the end of ary as starting pos.
- ;param1: takes int (dword) variable into eax
- ;param2: takes 'BYTE x dup (?)' char array into esi
- ;--------------------------------------------------------------
- itoa proc
- push ecx
- nextdigit: MOV edx,0
- MOV ebx, 10
- DIV ebx ;divide eax by 10 chops off last digit?
- ADD dl, 30h ;30h is zero?
- DEC esi
- MOV [esi], dl
- CMP eax, 0
- JNE nextdigit
- pop ecx
- ret
- itoa endp
- ;---------------------------------atoi-----------------------------
- ;converts ary of ascii to int
- ;param1: takes byte array into esi
- ;returns result in eax
- ;condition: stops when does not find an ascii digit 0-9
- ;----------------------------------------------------------------
- atoi proc
- MOV temp,10
- MOV ebx,0
- MOV eax,0
- nextDigit: MOV bl,[esi]
- CMP bl, '0'
- JL notADigit
- CMP bl,'9'
- JG notADigit
- ADD bl,-30h
- IMUL temp
- ADD eax, ebx
- INC esi
- JMP nextDigit
- notADigit: ret
- atoi endp
- ;---------------------------PrintToFile-----------------------------
- ;prints ascii text to output file until NULL is hit
- ;param1: takes byte array into esi
- ;condition: must already have var called outfile open and set
- ; to valid file handle
- ;------------------------------------------------------------------
- PrintToFile proc
- push ecx
- nextchar: MOV dl, [esi]
- CMP dl, NULL
- JE exitloop
- MOV edx, esi
- MOV ecx,1
- MOV eax,fileHandle
- CALL WriteToFile
- INC esi
- JNE nextchar
- exitloop:
- pop ecx
- ret
- PrintToFile endp
- ;------------------------- Blankout ---------------------------
- ;blanks out an array with spaces
- ;condition: must be a 'BYTE x dup (x)' of size x
- ;param1: takes 'BYTE x dup (x)' char array into esi
- ;--------------------------------------------------------------
- Blankout proc
- PUSH ecx
- MOV ecx,0
- next: MOV dl, 20h
- MOV [esi], dl
- INC esi
- INC ecx
- cmp ecx,1 ;size of ary
- JNE next
- POP ecx
- ret
- Blankout endp
- ;--------------------------RecursivePrint-------------------------
- ;prints out an array in order using recursion
- ;param1: beginning address of the array passed in on the stack
- ;----------------------------------------------------------------
- RecursivePrint proc
- A EQU 8[ebp]
- MOV eax,0
- ;necessary
- PUSH ebp
- MOV ebp,esp
- MOV esi,A
- MOV edi,esi ;uses this to increment elements later
- MOV al,[esi]
- ;if (ary[x] = NULL)
- CMP al,NULL
- JE BC
- ;else print ary element
- LEA esi,printNum
- MOV [esi],al
- CALL PrintToFile
- ;move to the next element in the ary
- INC edi
- PUSH edi
- CALL RecursivePrint
- BC:
- MOV esp,ebp
- POP ebp
- ret ; don't think I need to return a num bc this is void function
- RecursivePrint endp
- ;--------------------------RecursivePrintRev---------------------
- ;prints out an ary in reverse order using recursion
- ;param1: beginning element of ary passed in on the stack
- ;----------------------------------------------------------------
- RecursivePrintRev proc
- MOV eax,0
- ;necessary
- PUSH ebp
- MOV ebp,esp
- MOV esi,A
- MOV edi,esi ;uses this to increment elements later
- MOV al,[esi]
- ;if (ary[x] = NULL)
- CMP al,NULL
- JE BC
- ;move to the next element in the ary
- INC edi
- PUSH edi
- CALL RecursivePrintRev
- ;else print ary element
- LEA esi,printNum
- MOV al,A
- MOV [esi],al
- CALL PrintToFile
- BC:
- MOV esp,ebp
- POP ebp
- ret ; don't think I need to return a num bc this is void function
- RecursivePrintRev endp
- end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement