Difference between revisions of "CSC231 Homework 5 Fall 2017"

From dftwiki3
Jump to: navigation, search
(Problem 4)
Line 216: Line 216:
 
=Problem 4=
 
=Problem 4=
 
<br />
 
<br />
Stay tuned!
+
Create a new program in your account, called '''hw5dprep.asm''', and store the code below in it.
 
<br />
 
<br />
 +
::<source lang="asm">
 +
;;; ; hw5d_prep.asm
 +
;;; ; D. Thiebaut
 +
;;; ;
 +
;;; ;    nasm -f elf -F  stabs hw5d_prep.asm
 +
;;; ;    ld -melf_i386 -o hw5d_prep 231Lib.o hw5d_prep.o
 +
;;; ;    ./hw5d_prep
 +
;;; ; -------------------------------------------------------------------
 +
 +
;;;  ------------------------------------------------------------
 +
;;;  data areas
 +
;;;  ------------------------------------------------------------
 +
 +
        section .data
 +
fileName db "dummy.txt", 0
 +
buffer db "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
db      "                                                          "
 +
 +
noCharsRead dd 0
 +
 +
;;;  ------------------------------------------------------------
 +
;;;  code area
 +
;;;  ------------------------------------------------------------
 +
 +
        section .text
 +
        global  _start
 +
 +
extern _printString
 +
extern _readFile
 +
extern _println
 +
 +
_start:
 +
mov eax, fileName ; pass address of file-name in eax
 +
mov ebx, buffer ; pass buffer address in ebx
 +
mov ecx, noCharsRead ; pass address of var that will contain
 +
; number of chars read in ecx
 +
 +
call _readFile ; read the file whose name was passed in
 +
; eax.  _readFile will read the file and
 +
; put all the bytes read into the buffer
 +
; it will also put the number of bytes read
 +
; into the variable noCharsRead.
 +
 +
;; print the contents of the buffer
 +
mov ecx, buffer
 +
mov edx, dword[noCharsRead]
 +
call _printString
 +
call _println
 +
 +
;;;  exit()
 +
 +
        mov    eax,1
 +
        mov    ebx,0
 +
        int    0x80 ; final system call
 +
</source>
 +
<br />
 +
* Create a text file called '''dummy.txt''' with emacs, and enter two or three lines of text.  Maybe the beginning of your favorite poem.
 +
* Assemble and link with the 231Lib library (make sure you '''getcopy''' the new version of the 231Lib.asm library, and that you assemble the new library again).
 +
* Run the hw5dprep executable, and verify that it prints the contents of the ''dummy.txt'' file.
 +
<br />
 +
 
=Problem 5=
 
=Problem 5=
 
<br />
 
<br />
 
Stay tuned!
 
Stay tuned!
 
<br />
 
<br />

Revision as of 13:42, 30 October 2017

--D. Thiebaut (talk) 09:27, 30 October 2017 (EDT)



This assignment is due Monday 11/6/2017 at 11:55 p.m.



Preparation


For this assignment you will need to downloaded an updated version of 231Lib.asm, which has been augmented with a new function for reading strings.

getcopy 231Lib.asm

An example program using the new function, _getString(), is shown below:

;;; ; hw5Prep.asm
;;; ; D. Thiebaut
;;; ;
;;; ; Gets a string from the user and prints it back
;;; ;
;;; ; to assemble and run:
;;; ;
;;; ;     nasm -f elf  hw5Prep.asm
;;; ;     nasm -f elf  231Lib.asm
;;; ;     ld -melf_i386 -o hw5Prep hw5Prep.o 231Lib.o
;;; ;     ./hw5Prep
;;; ; -------------------------------------------------------------------

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

	        section .data
prompt		db	"> "

	
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	        section .text
	        global  _start
		extern	_getString
		extern	_printString
		extern	_println
	
_start:
                ;; prompt the user 
		mov	ecx, prompt
		mov	edx, 2
		call	_printString

                ;; get a string from the user
                ;; _getString returns the address of the string read in ecx,
                ;; and the number of chars read in edx
		call	_getString

                ;; since ecx and edx already contain the address and number
                ;; of chars, we can directly call _printString, which needs
                ;; the string and number of chars in the same registers
		call	_printString

                ;; print a line-feed char.
		call	_println


;;;  exit()

	        mov     eax,1
	        mov     ebx,0
	        int     0x80	; final system call


Create a version of this program in your account, and play with it. Verify that it gets a string from you and prints it back.

Notes


  1. _getString saves the string in an array declared inside 231Lib.asm. This array contains at most 1000 bytes.
  2. when _getString returns to your main program, it will set ecx to the address of the array containing the string, and it will set edx to contain the number of characters the user typed at the keyboard.
  3. the user indicates the end of the string by pressing the ENTER key. The string does not contain the ENTER character (line-feed).
  4. although I didn't do that here because the program is so simple and short, it is a good idea, just after returning from a call to _getString, to save ecx and edx into two different variables in memory that will hold the address of the string and the number of chars entered at the keyboard.


Problem 1


Write a program called hw5a.asm that prompts the user for a string, and prints it back several times. The number of times the string is printed is the same as the number of characters in the string.

Examples
cs231a@aurora ~ $ ./hw5a
> B
B
cs231a@aurora ~ $ ./hw5a
> a
a
cs231a@aurora ~ $ ./hw5a
> hello
hello
hello
hello
hello
hello
cs231a@aurora  $ ./hw5a
> chocolate
chocolate
chocolate
chocolate
chocolate
chocolate
chocolate
chocolate
chocolate
chocolate
cs231a@aurora  $ ./hw5a
> Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
Hall o ween
 


Note


  • Your program does not have to handle 0-length strings. In other words, your program will not be tested with empty strings.


Submission


  • Submit your program in the Homework 5, Problem 1 section on Moodle.


Problem 2


Modify your solution program for Problem 1 so that it removes the last character of the previous printed string as it goes through the loop:

Examples
cs231a@aurora  $ ./hw5b
> Hall-o-ween
Hall-o-ween
Hall-o-wee
Hall-o-we
Hall-o-w
Hall-o-
Hall-o
Hall-
Hall
Hal
Ha
H
cs231a@aurora  $ ./hw5b
> a
a
cs231a@aurora ~/HWs/HW5/PB2 $ ./hw5b
> 123456789
123456789
12345678
1234567
123456
12345
1234
123
12
1


Submission


  • Submit your program in the Homework 5, Problem 2 section on Moodle.


Problem 3


Same as with Problem 2, but this time the triangle is reversed:

Example
cs231a@aurora  $ ./hw5c
> Hal-lo-we-en
H
Ha
Hal
Hal-
Hal-l
Hal-lo
Hal-lo-
Hal-lo-w
Hal-lo-we
Hal-lo-we-
Hal-lo-we-e
Hal-lo-we-en


Submission


  • Submit your program to the Homework 5, Problem 3 section on Moodle.


Problem 4


Create a new program in your account, called hw5dprep.asm, and store the code below in it.

;;; ; hw5d_prep.asm
;;; ; D. Thiebaut
;;; ;
;;; ;     nasm -f elf -F  stabs hw5d_prep.asm
;;; ;     ld -melf_i386 -o hw5d_prep 231Lib.o hw5d_prep.o
;;; ;     ./hw5d_prep
;;; ; -------------------------------------------------------------------

;;;  ------------------------------------------------------------
;;;  data areas
;;;  ------------------------------------------------------------

	        section .data
fileName	db	"dummy.txt", 0
buffer		db	"                                                          "
		db      "                                                          "
		db      "                                                          "
		db      "                                                          "
		db      "                                                          "
		db      "                                                          "
		db      "                                                          "
		db      "                                                          "

noCharsRead	dd	0
	
;;;  ------------------------------------------------------------
;;;  code area
;;;  ------------------------------------------------------------

	        section .text
	        global  _start
	
extern	_printString
extern	_readFile
extern	_println
	
_start:
		mov	eax, fileName 			; pass address of file-name in eax
		mov	ebx, buffer			; pass buffer address in ebx
		mov	ecx, noCharsRead		; pass address of var that will contain
							; number of chars read in ecx
	
		call	_readFile 			; read the file whose name was passed in
							; eax.  _readFile will read the file and
							; put all the bytes read into the buffer 	 
							; it will also put the number of bytes read
							; into the variable noCharsRead.

	;; print the contents of the buffer
		mov	ecx, buffer
		mov	edx, dword[noCharsRead]
		call	_printString
		call	_println
	
;;;  exit()

	        mov     eax,1
	        mov     ebx,0
	        int     0x80	; final system call


  • Create a text file called dummy.txt with emacs, and enter two or three lines of text. Maybe the beginning of your favorite poem.
  • Assemble and link with the 231Lib library (make sure you getcopy the new version of the 231Lib.asm library, and that you assemble the new library again).
  • Run the hw5dprep executable, and verify that it prints the contents of the dummy.txt file.


Problem 5


Stay tuned!