Difference between revisions of "CSC231 Homework 7 Fall 2012"
(→Submission) |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 11: | Line 11: | ||
x = 305441741 = 0x1234ABCD | x = 305441741 = 0x1234ABCD | ||
+ | where 305441741 is printed by a call to '''print_int''', and 0x1234ABCD is printed by your program. | ||
==Submission== | ==Submission== | ||
Line 30: | Line 31: | ||
<br /> | <br /> | ||
− | Concentrate on the hexadecimal code for each letter. You will notice that 'A' through 'O' have the same upper nybble. 'A' is 0x'''4'''1 and 'O' is 0x'''4'''F. So, if I wanted to ''squeeze'' a string made of upper-case letters between 'A' and 'O', I | + | Concentrate on the hexadecimal code for each letter. You will notice that 'A' through 'O' have the same upper nybble. 'A' is 0x'''4'''1 and 'O' is 0x'''4'''F. So, if I wanted to ''squeeze'' a string made of upper-case letters between 'A' and 'O', I could keep only the lower nybbles, pack them, and I'd save some memory! |
The purpose of this problem is for you to take a string created using this method, decode it, and print it on the screen. | The purpose of this problem is for you to take a string created using this method, decode it, and print it on the screen. | ||
− | |||
− | |||
− | |||
− | |||
Line 42: | Line 39: | ||
x db 0xd1, 0x41, 0xd5, 0x03, 0x81, 0xee, 0x5c, 0x0d | x db 0xd1, 0x41, 0xd5, 0x03, 0x81, 0xee, 0x5c, 0x0d | ||
db 0x55, 0x40, 0x21, 0x40, 0xd1, 0xe0, 0x9e, 0x0c, 0x9d, 0x10 | db 0x55, 0x40, 0x21, 0x40, 0xd1, 0xe0, 0x9e, 0x0c, 0x9d, 0x10 | ||
− | + | ||
+ | |||
+ | Let's look at an example. Look at the 2 variables above. '''xLen''' is a double word, and contains 18. 18 represents the '''number of bytes''' or the '''number of characters''' in the original string. Since each byte of '''x''' contains 2 nybbles that are the lower parts of two consecutive characters, then '''xLen''' is the number of pairs of nybbles. | ||
+ | |||
+ | Let's take a look at '''x''' now. The first byte of '''x''' is 0xd1, which means that the first two characters of the original string are 0x4'''d''' and 0x4'''1''', or 'M' and 'A'. So your program will output 'M' then 'A' as the first two characters when it decodes the string. | ||
+ | |||
+ | ==Your Assignment== | ||
Your program must decode the two strings listed below and print them on the screen. Your program should replace the character '@' with a space when it prints it. | Your program must decode the two strings listed below and print them on the screen. Your program should replace the character '@' with a space when it prints it. | ||
Line 49: | Line 52: | ||
y db 0xd5, 0x54, 0x00, 0x31, 0xe4, 0x94, 0x0f, 0x21, 0xd1 | y db 0xd5, 0x54, 0x00, 0x31, 0xe4, 0x94, 0x0f, 0x21, 0xd1 | ||
db 0x01, 0xe4, 0x02, 0xfe, 0x90, 0xf6, 0x07, 0x1e, 0x10 | db 0x01, 0xe4, 0x02, 0xfe, 0x90, 0xf6, 0x07, 0x1e, 0x10 | ||
− | + | ||
− | + | zLen dd 24 | |
− | db | + | z db 0xef, 0x07, 0xff, 0x40, 0xd1, 0x41, 0xd5, 0x03, 0x81 |
+ | db 0xee, 0x5c, 0x01, 0xe4, 0x02, 0x14, 0x0d, 0x1e, 0x0d | ||
+ | db 0xf1, 0xe0, 0x9e, 0x0c, 0x9d, 0x10 | ||
<br /> | <br /> | ||
Line 66: | Line 71: | ||
=Python Program= | =Python Program= | ||
<source lang="python"> | <source lang="python"> | ||
− | + | text = "no good madame channel and bad man moan in lima" | |
− | text = "madame channel meet bad man in lima" | + | #text = "madame channel meet bad man in lima" |
− | text = "meet candid obama and boni of gana" | + | #text = "meet candid obama and boni of gana" |
text = text.upper() | text = text.upper() | ||
print( len(text), text ) | print( len(text), text ) | ||
− | + | ||
− | + | ||
lastNybble = ' ' | lastNybble = ' ' | ||
out = [] | out = [] | ||
− | + | ||
if len( text )%2 == 1: | if len( text )%2 == 1: | ||
text += "@" | text += "@" | ||
− | + | ||
count = 0 | count = 0 | ||
for i, c in enumerate( text ): | for i, c in enumerate( text ): | ||
Line 90: | Line 95: | ||
count += 1 | count += 1 | ||
lastNybble = nybble | lastNybble = nybble | ||
+ | |||
+ | |||
+ | print( "xLen dd ", count ) | ||
+ | print( "x db ", ', '.join( out ) ) | ||
+ | </source> | ||
− | + | * Other version where we create an array of the characters that are used and then pack the nybbles representing the indexes of the characters used in the string. | |
− | |||
+ | <source lang="python"> | ||
+ | #text = "no good madame channel and bad man moan in lima" | ||
+ | #text = "madame channel meet bad man in lima" | ||
+ | #text = "meet candid obama and boni of gana" | ||
+ | text = "A man, a plan, a canal, Panama" | ||
+ | text = text.upper() | ||
+ | print( len(text), text ) | ||
+ | |||
+ | letters = [] | ||
+ | for letter in text: | ||
+ | if not letter in letters: | ||
+ | letters.append( letter ) | ||
+ | |||
+ | array = [] | ||
+ | for letter in letters: | ||
+ | array.append( letter ) | ||
+ | print( "table db " + ', '.join( ["'"+k+"'" for k in array] ) ) | ||
+ | |||
+ | lastNybble = ' ' | ||
+ | out = [] | ||
+ | |||
+ | if len( text )%2 == 1: | ||
+ | text += "@" | ||
+ | |||
+ | count = 0 | ||
+ | for i, c in enumerate( text ): | ||
+ | #if c == ' ': c = '@' | ||
+ | index = letters.index( c ) | ||
+ | nybble = hex( index )[-1] | ||
+ | #nybble = index | ||
+ | print( c, hex( ord( c ) ), nybble ) | ||
+ | if i!= 0 and i%2==1: | ||
+ | hexa = "0x" + lastNybble + nybble | ||
+ | out.append( hexa ) | ||
+ | print( " 0x" + lastNybble + nybble ) | ||
+ | count += 1 | ||
+ | lastNybble = nybble | ||
+ | |||
+ | |||
+ | print( "xLen dd ", count ) | ||
+ | print( "x db ", ', '.join( out ) ) | ||
</source> | </source> | ||
+ | |||
</onlydft> | </onlydft> | ||
Latest revision as of 14:15, 30 October 2012
--D. Thiebaut 13:47, 24 October 2012 (EDT)
This assignment is due the evening of Oct. 31st, at midnight (booo!). You can work on this assignment in pairs.
Contents
Problem #1 (0.5 point doc, 1.5 points program)
Write a program that displays the contents of a double-word in hexadecimal. Your program should store the value 0x1234ABCD in a variable called x and should display this information:
x = 305441741 = 0x1234ABCD
where 305441741 is printed by a call to print_int, and 0x1234ABCD is printed by your program.
Submission
Submit your program (called hw7a.asm) as follows:
rsubmit hw7 hw7a.asm
Hints
HexChars db "0123456789ABCDEF"
Problem #2 (0.5 point doc, 1.5 points program)
Take a look at the section of the ASCII table below:
Concentrate on the hexadecimal code for each letter. You will notice that 'A' through 'O' have the same upper nybble. 'A' is 0x41 and 'O' is 0x4F. So, if I wanted to squeeze a string made of upper-case letters between 'A' and 'O', I could keep only the lower nybbles, pack them, and I'd save some memory!
The purpose of this problem is for you to take a string created using this method, decode it, and print it on the screen.
xLen dd 18 x db 0xd1, 0x41, 0xd5, 0x03, 0x81, 0xee, 0x5c, 0x0d db 0x55, 0x40, 0x21, 0x40, 0xd1, 0xe0, 0x9e, 0x0c, 0x9d, 0x10
Let's look at an example. Look at the 2 variables above. xLen is a double word, and contains 18. 18 represents the number of bytes or the number of characters in the original string. Since each byte of x contains 2 nybbles that are the lower parts of two consecutive characters, then xLen is the number of pairs of nybbles.
Let's take a look at x now. The first byte of x is 0xd1, which means that the first two characters of the original string are 0x4d and 0x41, or 'M' and 'A'. So your program will output 'M' then 'A' as the first two characters when it decodes the string.
Your Assignment
Your program must decode the two strings listed below and print them on the screen. Your program should replace the character '@' with a space when it prints it.
yLen dd 18 y db 0xd5, 0x54, 0x00, 0x31, 0xe4, 0x94, 0x0f, 0x21, 0xd1 db 0x01, 0xe4, 0x02, 0xfe, 0x90, 0xf6, 0x07, 0x1e, 0x10 zLen dd 24 z db 0xef, 0x07, 0xff, 0x40, 0xd1, 0x41, 0xd5, 0x03, 0x81 db 0xee, 0x5c, 0x01, 0xe4, 0x02, 0x14, 0x0d, 0x1e, 0x0d db 0xf1, 0xe0, 0x9e, 0x0c, 0x9d, 0x10
Submission
rsubmit hw7 hw7b.asm