Difference between revisions of "CSC231 Homework 4 Fall 2017"
(13 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
cs231a@aurora ~/ $ ld -melf_i386 hw4a.o 231Lib.o -o hw4a | cs231a@aurora ~/ $ ld -melf_i386 hw4a.o 231Lib.o -o hw4a | ||
cs231a@aurora ~/ $ ./hw4a | cs231a@aurora ~/ $ ./hw4a | ||
− | > | + | > 12345 |
− | + | 0...030071 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | I used ellipsis in the output above to mask the number of leading 0s actually output by the program. It's part of your assignment to figure how many leading zeros must be printed. Be resourceful! | ||
+ | |||
<br /> | <br /> | ||
==Details== | ==Details== | ||
Line 45: | Line 23: | ||
* Your program gets the input using the _getInput() function in the 231Lib.asm library | * Your program gets the input using the _getInput() function in the 231Lib.asm library | ||
* Your program outputs the octal equivalent, padded with leading 0s. | * Your program outputs the octal equivalent, padded with leading 0s. | ||
− | |||
<br /> | <br /> | ||
Line 52: | Line 29: | ||
Submit your program on Moodle. Make sure you test it well before submitting it. In particular, if you add comments to your program just before submitting it, you should assemble, link and run it at least once to make sure you didn't change the logic of your program by adding comments. | Submit your program on Moodle. Make sure you test it well before submitting it. In particular, if you add comments to your program just before submitting it, you should assemble, link and run it at least once to make sure you didn't change the logic of your program by adding comments. | ||
<br /> | <br /> | ||
+ | |||
=Problem 2= | =Problem 2= | ||
<br /> | <br /> | ||
+ | Your assignment is to write an assembly program named '''hw4b.asm''' that can be used to decipher a secret message. | ||
+ | |||
+ | Your program will read a 32-bit integer from the user, decipher it (explained shortly), and print the corresponding string. We assume that the integer will be unsigned, for this assignment. For example: | ||
+ | |||
+ | ./hw4b | ||
+ | > 1094861636 | ||
+ | ABCD | ||
+ | |||
+ | In hexadecimal, 1094861636 is 0x41424344, where you will recognize 41 as the hexadecimal ascii for 'A', 42 the ascii for 'B', 43 the ascii for 'C', and 44 the ascii for 'D'. Hence the output "ABCD." | ||
+ | |||
+ | Note that the program outputs a line-feed after the string. | ||
+ | |||
+ | Another example: | ||
+ | |||
+ | ./hw4b | ||
+ | > 1131376492 | ||
+ | Cool | ||
+ | |||
+ | <br /> | ||
+ | ==Implementation Details== | ||
+ | <br /> | ||
+ | * Your program should be linked with the 231Lib library. | ||
+ | * Your program must output a line-feed at the end of the 4-character message. | ||
+ | <br /> | ||
+ | ==Submission== | ||
+ | <br /> | ||
+ | * Submit your program on Moodle, in the Homework 4, Problem 2 section. | ||
+ | <br /> | ||
+ | |||
=Problem 3= | =Problem 3= | ||
<br /> | <br /> | ||
+ | Write a series of bash commands that will print four triangles of 5 lines made of the , '2', '3', '1', and 'a' characters, as shown below: | ||
+ | |||
+ | ::<source lang="text"> | ||
+ | 2 | ||
+ | 22 | ||
+ | 222 | ||
+ | 2222 | ||
+ | 22222 | ||
+ | |||
+ | 3 | ||
+ | 33 | ||
+ | 333 | ||
+ | 3333 | ||
+ | 33333 | ||
+ | |||
+ | 1 | ||
+ | 11 | ||
+ | 111 | ||
+ | 1111 | ||
+ | 11111 | ||
+ | |||
+ | a | ||
+ | aa | ||
+ | aaa | ||
+ | aaaa | ||
+ | aaaaa | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Requirements== | ||
+ | <br /> | ||
+ | * To get full credits, your program must contain only three nested '''for''' loops. No fewer than 3, no more than 3. | ||
+ | * There must be a blank line between each triangle, and at the end of the last triangle. | ||
+ | <br /> | ||
+ | |||
+ | ==Submission== | ||
+ | <br /> | ||
+ | * Copy paste or write your commands in a text file called '''hw4c.sh''' (note the different extension). | ||
+ | * Submit it to Moodle, in the Homework 4, Problem 3 section. | ||
+ | <br /> | ||
+ | ==Testing== | ||
+ | <br /> | ||
+ | I created a program that will be used by the autograder to verify that your program works correctly. You can run it and it will automatically test your '''hw4c.sh''' file. Just type '''testHw4c.py''' at the Linux prompt. It will automatically look for your hw4c.sh file and verify that it contains the correct Linux commands, and that it generates the correct output: | ||
+ | |||
+ | testHw4c.py | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | =Solutions= | ||
+ | <br /> | ||
+ | ==Problem 1== | ||
+ | <br /> | ||
+ | ::<source lang="asm"> | ||
+ | ;;; # hw4a.asm | ||
+ | ;;; D. Thiebaut | ||
+ | ;;; this program takes as input a 32-bit int entered by | ||
+ | ;;; the user responding to a call to _getInput() and | ||
+ | ;;; prints out the octal version of the integer. | ||
+ | ;;; | ||
+ | |||
+ | section .data | ||
+ | x dd 0 ; the int entered by the user | ||
+ | d0 db "37777777777" ; string for all the octal digits | ||
+ | lf db 10 ; line-feed char | ||
+ | octLen equ $-d0 ; length of string to print (plus lf) | ||
+ | prompt db '> ' ; prompt used to get input | ||
+ | |||
+ | extern _printInt | ||
+ | extern _getInput | ||
+ | extern _printString | ||
+ | extern _println | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | ;;; prompt | ||
+ | mov ecx, prompt | ||
+ | mov edx, 2 | ||
+ | call _printString | ||
+ | |||
+ | ;;; read int | ||
+ | call _getInput | ||
+ | mov dword[x], eax | ||
+ | |||
+ | ;;; divide into 11 digits | ||
+ | mov ecx, 11 | ||
+ | mov eax, dword[x] | ||
+ | mov ebx, 8 | ||
+ | |||
+ | ;;; loop through all the digits and store the digits | ||
+ | ;;; in the string. This solution uses a loop, which | ||
+ | ;;; we hadn't seen yet. This provides advanced material | ||
+ | ;;; on how to loop through an array. | ||
+ | for: mov edx, 0 | ||
+ | div ebx | ||
+ | add dl, '0' | ||
+ | mov byte[d0+ecx-1], dl | ||
+ | loop for | ||
+ | |||
+ | ;;; print octal | ||
+ | mov ecx, d0 | ||
+ | mov edx, octLen | ||
+ | call _printString | ||
+ | |||
+ | ;;; exit | ||
+ | mov eax,1 | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Problem #2== | ||
+ | <br /> | ||
+ | ::<source lang="asm"> | ||
+ | ;;; hw4b.asm | ||
+ | ;;; D. Thiebaut | ||
+ | ;;; Takes an integer from the user, as a 32-bit int, | ||
+ | ;;; stores it in memory, and prints the 4 bytes forming | ||
+ | ;;; the number as ASCII chars, in reverse order, i.e. the | ||
+ | ;;; most-significant byte first. | ||
+ | |||
+ | section .data | ||
+ | x dd 0 | ||
+ | lf db 10 | ||
+ | prompt db '> ' | ||
+ | |||
+ | extern _printInt | ||
+ | extern _getInput | ||
+ | extern _printString | ||
+ | extern _println | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | ;;; prompt | ||
+ | mov ecx, prompt | ||
+ | mov edx, 2 | ||
+ | call _printString | ||
+ | |||
+ | ;;; read int | ||
+ | call _getInput | ||
+ | mov dword[x], eax | ||
+ | |||
+ | ;;; swap bytes around, so that the most significant byte | ||
+ | ;;; becomes the least significant byte | ||
+ | mov al, byte[x] | ||
+ | mov bl, byte[x+1] | ||
+ | mov cl, byte[x+2] | ||
+ | mov dl, byte[x+3] | ||
+ | mov byte[x+3],al | ||
+ | mov byte[x+2],bl | ||
+ | mov byte[x+1],cl | ||
+ | mov byte[x],dl | ||
+ | |||
+ | ;;; then print the 4-bytes as if they were a string. Print | ||
+ | ;;; a line-feed at the end. | ||
+ | mov ecx, x | ||
+ | mov edx, 5 | ||
+ | call _printString | ||
+ | |||
+ | ;;; exit | ||
+ | mov eax,1 | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Problem #3== | ||
+ | <br /> | ||
+ | ::<source lang="bash"> | ||
+ | # hw4c.sh | ||
+ | # D. thiebaut | ||
+ | # uses for loops to print 4 triangles of characters | ||
+ | |||
+ | for i in '2' '3' '1' 'a' ; do | ||
+ | for line in `seq 1 5` ; do | ||
+ | for char in `seq 1 $line` ; do | ||
+ | echo -n $i | ||
+ | done | ||
+ | echo "" | ||
+ | done | ||
+ | echo "" | ||
+ | done | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ===Output=== | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | 2 | ||
+ | 22 | ||
+ | 222 | ||
+ | 2222 | ||
+ | 22222 | ||
+ | |||
+ | 3 | ||
+ | 33 | ||
+ | 333 | ||
+ | 3333 | ||
+ | 33333 | ||
+ | |||
+ | 1 | ||
+ | 11 | ||
+ | 111 | ||
+ | 1111 | ||
+ | 11111 | ||
+ | |||
+ | a | ||
+ | aa | ||
+ | aaa | ||
+ | aaaa | ||
+ | aaaaa | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC231]][[Category:Homework]][[Category:Nasm]][[Category:Bash]] |
Latest revision as of 09:17, 24 October 2017
--D. Thiebaut (talk) 15:15, 13 October 2017 (EDT)
Contents
Problem 1
Write an assembly language program called hw4a.asm that translate a decimal unsigned integer into its octal equivalent.
Here is an example of how it should behave:
cs231a@aurora ~/ $ nasm -f elf hw4a.asm cs231a@aurora ~/ $ ld -melf_i386 hw4a.o 231Lib.o -o hw4a cs231a@aurora ~/ $ ./hw4a > 12345 0...030071
I used ellipsis in the output above to mask the number of leading 0s actually output by the program. It's part of your assignment to figure how many leading zeros must be printed. Be resourceful!
Details
- Your program must be linked with the 231Lib.asm library
- Your program will print a prompt ( "> " ) first, then the user enters a positive number (it will never be negative)
- Your program gets the input using the _getInput() function in the 231Lib.asm library
- Your program outputs the octal equivalent, padded with leading 0s.
Submission
Submit your program on Moodle. Make sure you test it well before submitting it. In particular, if you add comments to your program just before submitting it, you should assemble, link and run it at least once to make sure you didn't change the logic of your program by adding comments.
Problem 2
Your assignment is to write an assembly program named hw4b.asm that can be used to decipher a secret message.
Your program will read a 32-bit integer from the user, decipher it (explained shortly), and print the corresponding string. We assume that the integer will be unsigned, for this assignment. For example:
./hw4b > 1094861636 ABCD
In hexadecimal, 1094861636 is 0x41424344, where you will recognize 41 as the hexadecimal ascii for 'A', 42 the ascii for 'B', 43 the ascii for 'C', and 44 the ascii for 'D'. Hence the output "ABCD."
Note that the program outputs a line-feed after the string.
Another example:
./hw4b > 1131376492 Cool
Implementation Details
- Your program should be linked with the 231Lib library.
- Your program must output a line-feed at the end of the 4-character message.
Submission
- Submit your program on Moodle, in the Homework 4, Problem 2 section.
Problem 3
Write a series of bash commands that will print four triangles of 5 lines made of the , '2', '3', '1', and 'a' characters, as shown below:
2 22 222 2222 22222 3 33 333 3333 33333 1 11 111 1111 11111 a aa aaa aaaa aaaaa
Requirements
- To get full credits, your program must contain only three nested for loops. No fewer than 3, no more than 3.
- There must be a blank line between each triangle, and at the end of the last triangle.
Submission
- Copy paste or write your commands in a text file called hw4c.sh (note the different extension).
- Submit it to Moodle, in the Homework 4, Problem 3 section.
Testing
I created a program that will be used by the autograder to verify that your program works correctly. You can run it and it will automatically test your hw4c.sh file. Just type testHw4c.py at the Linux prompt. It will automatically look for your hw4c.sh file and verify that it contains the correct Linux commands, and that it generates the correct output:
testHw4c.py
Solutions
Problem 1
;;; # hw4a.asm ;;; D. Thiebaut ;;; this program takes as input a 32-bit int entered by ;;; the user responding to a call to _getInput() and ;;; prints out the octal version of the integer. ;;; section .data x dd 0 ; the int entered by the user d0 db "37777777777" ; string for all the octal digits lf db 10 ; line-feed char octLen equ $-d0 ; length of string to print (plus lf) prompt db '> ' ; prompt used to get input extern _printInt extern _getInput extern _printString extern _println section .text global _start _start: ;;; prompt mov ecx, prompt mov edx, 2 call _printString ;;; read int call _getInput mov dword[x], eax ;;; divide into 11 digits mov ecx, 11 mov eax, dword[x] mov ebx, 8 ;;; loop through all the digits and store the digits ;;; in the string. This solution uses a loop, which ;;; we hadn't seen yet. This provides advanced material ;;; on how to loop through an array. for: mov edx, 0 div ebx add dl, '0' mov byte[d0+ecx-1], dl loop for ;;; print octal mov ecx, d0 mov edx, octLen call _printString ;;; exit mov eax,1 mov ebx,0 int 0x80 ; final system call
Problem #2
;;; hw4b.asm ;;; D. Thiebaut ;;; Takes an integer from the user, as a 32-bit int, ;;; stores it in memory, and prints the 4 bytes forming ;;; the number as ASCII chars, in reverse order, i.e. the ;;; most-significant byte first. section .data x dd 0 lf db 10 prompt db '> ' extern _printInt extern _getInput extern _printString extern _println section .text global _start _start: ;;; prompt mov ecx, prompt mov edx, 2 call _printString ;;; read int call _getInput mov dword[x], eax ;;; swap bytes around, so that the most significant byte ;;; becomes the least significant byte mov al, byte[x] mov bl, byte[x+1] mov cl, byte[x+2] mov dl, byte[x+3] mov byte[x+3],al mov byte[x+2],bl mov byte[x+1],cl mov byte[x],dl ;;; then print the 4-bytes as if they were a string. Print ;;; a line-feed at the end. mov ecx, x mov edx, 5 call _printString ;;; exit mov eax,1 mov ebx,0 int 0x80 ; final system call
Problem #3
# hw4c.sh # D. thiebaut # uses for loops to print 4 triangles of characters for i in '2' '3' '1' 'a' ; do for line in `seq 1 5` ; do for char in `seq 1 $line` ; do echo -n $i done echo "" done echo "" done
Output
2 22 222 2222 22222 3 33 333 3333 33333 1 11 111 1111 11111 a aa aaa aaaa aaaaa