(Задание 01 курса Assembly Language)
Part I. An Exploration. Not to be handed in.
a) Write a short program that consists of variations of the mov instruction using the different modes
For example:
label1: mov AX,BX
label2: mov BX,AX
label3: mov AX, 5
label4: mov AX,127
label5: mov AX, item
label6: mov item,AX
etc.etc.etc
Assemble your code with the list option and print out the .lst file.
Look at the file to explore how the machine language works. How many bytes in each instruction.? Is there any clear mechanism for generating the op-codes? How are the operands stored in the machine code instructions e.g. in the register mode, immediate mode , direct mode, indirect mode. What are the offsets for the various labels and what does that say about the instruction lengths.
Add a few items to the data section and construct the symbol table. Run the program using the debugger (see the book appendix) and look at the values of the various segment registers.
Part II. To be handed in.
Write a stand-alone assembly language program that inputs two characters using the software interrupt 21 mechanism and calculates the Hamming distance between the two characters.
First, however, in order to get practice using arrays, each character MUST be unpacked into an array of 8 bytes where each byte contains the bit value (either 0 or 1). Then the Hamming distance should be calculated. The Hamming distance is the number of digit positions in which the corresponding digits of two binary patterns of the same length are different. For example, the Hamming distance between 1011101 and 1001001 is two.
After calculating the Hamming difference for a pair of characters, your program should query the user whether he/she wants to enter another pair of characters. If the answer is ‘yes’ (you can use Y/N for ‘yes’ or ‘no’), read and process another character pair. The program terminates only when the user answers ‘no’ to the query.
You should use the I/O macros for all other Input/Output
Submit the following folder via Raven:
Files containing the source code and the executable.
A file containing a Test plan and test data output: For each test case, provide the objective (why you are doing that particular test) and the rationale for selecting the test input. as well as the correct answer from the ASCII table. Zero marks will be given to this part if your assignment provides testing information without this information (i.e., objective and rationale). In particular identify the border cases and test them. Note this file should be clearly annotated so that the T.A. knows exactly what you are showing.
Run at least one run with the sets A,S A,B, 0,1 5,7 A,5 and show the output of your program with this test data . This is not sufficient testing however.
Marking scheme for this assignment (25 marks):
Correctness of the program: 10 marks (if your programs works correctly and your documentation convinces the TA, don’t expect marks if you just submit a print out of the source code)
Structure and comments: 3 marks (spacing, indentation, comments, variable names etc.)
Good, efficient clear code: 3 marks (weird or convoluted solutions will get zero)
Test plan & test data output: 6 marks (zero marks if no objective and rationale as mentioned above)
Output with class test data: 3 marks
NOTE: To submit your results you must capture the program output in the MS-DOS command window:
1) drag the desired text
2) click on the DOS drop down menu and select edit /copy
3) paste the text into an editor or word processor
Solutions
as2_t.asm
TITLE Assembly_assignment_1
COMMENT |
program inputs two characters using the software interrupt 21
mechanism and calculates the Hamming distance between the two characters.
|
.MODEL SMALL
.STACK 100H
.DATA
prompt_1 DB 'Please input a character 1: ',0
prompt_2 DB 'Please input a character 2: ',0
prompt_3 DB 'The hamming distance is: ', 0
quit_msg1 DB 'Do you want to quit (Y/N)?',0
array_1 DB 8 DUP (?) ; array for first bit pattern
array_2 DB 8 DUP (?) ; ...and second
.CODE
INCLUDE io.mac
.486
main PROC
.STARTUP
start:
; work with first character
PutStr prompt_1 ;ask to enter character
mov AH, 01H
int 21H
nwln
mov AH,80H ; bitmask 10000000
mov CX,8 ; loop counter
mov BX,OFFSET array_1
mov DX,1
print_binary_c1:
test AL,AH ; compare character to bitmask
jz byte_0_b1 ; if zero - go to print zero
PutCh '1' ; if not zero->it's one->print '1'
mov [BX], DX ; put 1 into array's element
jmp skip1_b1
byte_0_b1:
dec DX ; make DX=0
mov [BX], DX ; and store it into array
inc DX ; make DX 0 again for next iteration
PutCh '0' ; print 0
skip1_b1:
shr AH,1 ; shift bitmask to the right
inc BX ; make BX point to next element in an array
loop print_binary_c1
nwln
; work with second character
PutStr prompt_2
mov AH, 01H
int 21H
nwln
mov AH,80H
mov CX,8
mov BX,OFFSET array_2
mov DX,1
print_binary_c2:
test AL,AH
jz byte_0_b2
PutCh '1'
mov [BX], DX
jmp skip1_b2
byte_0_b2:
dec DX
mov [BX], DX
inc DX
PutCh '0'
skip1_b2:
shr AH,1
inc BX
loop print_binary_c2
mov CX, 0 ; counter for hamm loop
mov BX, 0 ; counter for hamming distance
calculate_hamm:
mov SI, OFFSET array_1
add SI, CX ; increment adress of array_2 with loop counter
mov DH, [SI] ; take one byte
cmp DH, 0 ; and compare it to zero
je bitzero
; the bit from array_1 is 1, check array_2's bit
mov SI, OFFSET array_2
add SI, CX ; increment adress of array_2 with loop counter
mov DH, [SI] ; take one byte
cmp DH, 1 ; and compare it to one
je equalbits ; if bytes are equal - jump to equalbits
inc BX ; if not - increment Hamming distance counter
inc CX ; increment counter
cmp CX, 8 ; compare it to 8
jl calculate_hamm ; and jump over again if less
bitzero: ; if byte is zero - take corresponding byte of another array and compare them
mov SI, OFFSET array_2
add SI, CX ; increment adress of array_2 with loop counter
mov DH, [SI] ; take one byte
cmp DH, 0 ; and compare it to zero
je equalbits ; if bytes are equal - jump to equalbits
inc BX ; if not - increment Hamming distance counter
inc CX ; increment counter
cmp CX, 8 ; compare it to 8
jl calculate_hamm ; and jump over again if less
equalbits: ; if bytes are equal - increment counter and go to next byte
inc CX
cmp CX, 8
jl calculate_hamm
nwln
nwln
PutStr prompt_3
PutInt BX
nwln
quit_prompt:
PutStr quit_msg1
mov AH, 01H
int 21H
nwln
cmp AL, 'y'
je theend
cmp AL, 'Y'
je theend
cmp AL, 'n'
je start
cmp AL, 'N'
je start
nwln
jmp quit_prompt
theend:
.EXIT
main ENDP
END main
Part 3 Test Test output for characters 'A' and 'S': -------------------------------------------- D:\>as2_t Please input a character 1: A 01000001 Please input a character 2: S 01010011 The hamming distance is: 2 Do you want to quit (Y/N)?y -------------------------------------------- Test output for characters 'A' and 'B': -------------------------------------------- D:\>as2_t Please input a character 1: A 01000001 Please input a character 2: B 01000010 The hamming distance is: 2 Do you want to quit (Y/N)?y -------------------------------------------- Test output for characters '0' and '1': -------------------------------------------- D:\>as2_t Please input a character 1: 0 00110000 Please input a character 2: 1 00110001 The hamming distance is: 1 Do you want to quit (Y/N)?y -------------------------------------------- Test output for characters '5' and '7': -------------------------------------------- D:\>as2_t Please input a character 1: 5 00110101 Please input a character 2: 7 00110111 The hamming distance is: 1 Do you want to quit (Y/N)?y -------------------------------------------- Test output for characters 'A' and '5': -------------------------------------------- D:\>as2_t Please input a character 1: A 01000001 Please input a character 2: 5 00110101 The hamming distance is: 4 Do you want to quit (Y/N)?y --------------------------------------------
1 Response to Задание 1: Hamming Distance, Ассемблер
» Описание курса Computer Science Student
Сентябрь 13th, 2009 at 10:04
[...] Задание 1: Hamming Distance, Ассемблер [...]