80x86 Assembly Language Revision Exercises
Transcription
80x86 Assembly Language Revision Exercises
Instructor: Dr Konstantinos Katzis Tel: 22-713296 e-mail: K.Katzis@euc.ac.cy Fall Semester 2009 80x86 Assembly Language Revision Exercises 1 1) What is the function of the CPU the instruction set? List 3 CPU components and briefly describe Machine Cycle. 2) Example: Suppose that EAX= 00002000h, EBX=00001000h, ESI=00002000h, EDI =00003000h and the memory locations 00001000h, 00002000h, 00003000h contain the values 00001BACh, 000020FEh and 0000031Dh respectively. For each of the following instruction write down the contents of the memory and the registers EBX,ECX,ESI,EDI. EBX EXC ESI EDI 0000 1000h ? 0000 2000h 0000 3000h 0000 1000h 0000 1BACh 0000 2000h 0000 3000h 0000 20FEh 0000 031Dh mov EBX,[EBX] mov ECX,[ESI] mov EBX,[EAX] add [ESI],[EDI] inc EDI inc [ESI] 3) Fill in the following registers Address 000010F0 000010F1 000010F2 000010F3 000010F4 000010F5 000010F6 000010F7 mov ESI,000010F2h mov AH,[ESI] inc ESI mov AH,[ESI] add ESI,4 mov AH,[ESI] mov AX,ESI Memory Contents 1Ch F3h 5Ah 19h 67h 42h 23h 45h ; AH now contains 5Ah ; ESI now contains __________ ; AH now contains __________ ; ESI now contains __________ ; AH now contains __________ ; AX now contains ___________ 2 4) Write code to define two arrays. The first one should be an array of bytes contain the values A0h, 2Ah, 0Dh and F0h. The second one should contain 3 un- initialized double words. Use a table to show the memory contents after the declarations. 5) Show how the following items will be stored in memory var1 BYTE 00001111b var2 BYTE 10 var3 BYTE “hello” var4 WORD 20 var5 DWORD 20 var6 BYTE –3 var7 WORD 2, 5h, 3, 001b 6) Answer the following questions related to the code listed below. 3 6a) Number1,number2 are double words. Number1 and Number 2 are saved in in memory locations 00000000 and 00000004. Why? 6b) Prompt1 starts at memory location _ _ _ _ _ _ _ _ and prompt2 start at memory location 0000001E 6c) Draw a table showing the contents of the memory after the declarations show in the data segment of exercise 6. Address Contents Address Contents Address Contents 4 7) Write a program in assembly to add three numbers input by the user, and output the result. 8) Explain the actions taking place in each of the following operations. Indicate any illegal operations .data b1 BYTE 4Fh b2 BYTE 3Ah w WORD 2048 .code mov bl,dh mov ax,w mov ch,b1 mov b2, b1 mov al,255 mov al, w mov w,-100 mov b,0 9) Fill in the following comments: Examples: .data b w ww BYTE 03 ;____________ WORD 08 ;____________ DWORD 12 ; _ _ _ _ _ _ _ _ _ _ _ _ .code mov ax, 05 add ax, w mov bl, 23 add w, bl mov EAX, 0 add EAX, ww ;____________ ;____________ ;____________ ;____________ ;____________ 5 10) Fill in the flag status: 11) Write code to calculate the expression: res=-2*(num1+num2-num3) +1 (Note: res, num1, num2 and num3 are WORDS stored in memory). 12) Describe the use of the following directives - part of an assembly code: a. .386 b. .MODEL FLAT c. .DATA 13) Fill in the following: Before AX: 00 05 BX: 00 02 DX: ?? ?? Instruction Executed imul bx After DX: __ __ AX: __ __ CF, OF: __ AX: ?? 05 Factor = FF imul Factor AX: __ __ CF, OF: __ EBX: 00 00 00 0A imul ebx,2 EBX : __ __ __ __ CF, OF: __ ECX: FF FF FF F4 imul ecx,Factor Factor=FF FF FF F2 ECX : __ __ __ __ CF, OF: __ Value= 00 F2 (242d) imul bx,Value, 2 BX: ?? ?? BX : 01 E4 CF, OF: __ 6 14) Write a program that calculates 1+2+3+4+5+6. . . The result (sum is saved in memory in a location shown by the label SUM) 15) Write the output of the following programs. (assume that the user inputs the numbers 3, 5 and 4 when requested) .DATA sum prompt number countLabel sumLabel avgLabel value nextPrompt .CODE _start: forever: ; reserve storage for data DWORD ? BYTE "number? ",0 BYTE 16 DUP (?) BYTE "count",0 BYTE " sum",0 BYTE " average",0 BYTE 11 DUP (?), 0 BYTE cr,Lf,Lf,"next ",0 ; start of main program code mov sum,0 mov ebx,0 ; sum := 0 ; count := 0 output prompt input number,16 atod number add sum,eax inc ebx ; prompt for number ; read ASCII characters ; convert to integer ; add number to sum ; add 1 to count dtoa value,ebx output countLabel output value ; convert count to ASCII ; display label for count ; display count dtoa value,sum output sumLabel output value mov eax,sum cdq idiv ebx dtoa value,eax output avgLabel output value ; convert sum to ASCII ; display label for sum ; display sum ; get sum ; extend sum to 64 bits ; sum / count ; convert average to ASCII ; display label for average ; output average output nextPrompt jmp forever PUBLIC _start END ; skip down, start next prompt ; repeat ; make entry point public 7 16) Write down the values of the ZF, CF and SF after each of the following instructions is executed. Indicate any illegal instructions .data num1 BYTE 30h num2 BYTE FFh .code ZF CF SF mov ah, 31h ; cmp ah, num1 ; cmp ah, 31h ; cmp num2, F0h ; cmp 40h, ah ; cmp ah, ‘A’ ; 17) Write code to add 50 to the current value of the ax register. If the result of the addition is positive set in the bx register the value 1. If the result of the addition is negative set in the bx register the value -1. 18) Write a program to compare the values in the AX and BX registers, and set the biggest value (among the two) in the CX register.) 19) Write assembly language code for the following statements, using Conditional JUMPS a=5 for i=0: until i=50 do a:=a+1 end 20) Write code to for performing the following actions: If AX < 0 then Set the number 5h in the BX register Else Set the number 22h in the BX register 21) Write code to calculate the sum of the following series: 1 + 4 + 7 + 10 + . . . ., until the sum becomes greater than 100. 22) Write code for the following statements if (value >=30) or (value<10) then value = 100 8 23) Write code for the following statements if (value >=30) AND (rate>10) then value = value-1 24) Describe in words the functionality of the following program. mov mov ADD_ONE: add add mov loop AX,00h ECX 20d AX,01h BX,01h ECX, 05h ADD_ONE ; reset AX ; set counter to 20 ; repeated 20 times 25) Write code to define two arrays of double words of 50 elements each. Also write code to copy the contents of the first array to the contents of the second array. 26) Write code to define an array of double words with the following items. f304, 00f5, 143d, c4f2. Also write code to calculate the sum of all array items and store the result in a variable called SUM. 27) Initially BX=6DA4, EAX=FD347CCC and ESP=000F1007. Show the contents of the stack and the ESP after the following instructions are executed. push BX push EAX Address 000F1008h 000F1007h 000F1006h 000F1005h 000F1004h 000F1003h 000F1002h 000F1001h 000F1000h 000F1008h Contents 23h FDh ESP=000F1007h 28) Let ESP=000F1000. Show the contents of the ax and ebx registers when the following instructions are executed pop AX pop EBX 9 Address 000F1008h 000F1007h 000F1006h 000F1005h 000F1004h 000F1003h 000F1002h 000F1001h 000F1000h Contents 23h FDh 43h F5h Dah AAh 6Dh 65h 09h 29) Write a procedure called swap that accepts the address of two words in the stack and swaps their values 30) Write code to move 20 bytes from STRING1 to STRING2 without using string operations. 31) Write a program to check whether a 10 character password entered by a user is the same with a pre-stored 10 character password. Assume that the pre-stored and entered passwords are already available in memory with the names passw and str. 32) Write a procedure that accepts a word in the AX register and returns in the EBX register the number of one’s in the word given. 33) Fill in the following: mov mov sal sar AH,01101101b CL,2 AH,1 AH,CL ; AH now contains ____________ ; AH now contains ____________ ;AH now contains ____________ ; AH now contains ____________ 34) Fill in the following: mov mov rol ror AH,01101101b CL,2 AH,1 AH,CL ; AH now contains ____________ ; AH now contains ____________ ;AH now contains ____________ ; AH now contains ____________ 35) Write a procedure to output on the screen the contents of the AH register in binary format. (Hint convert the contents of AH into an 8 byte string containing ‘0’ or ‘1’ characters. 36) What is RISC and CISC. List their advantages and disadvantages and compare the two. 10 37) List three advantages and three disadvantages of assembly. 38) Find the word-length 2’s complement representation of each of the following DECIMAL numbers: 845, 15000, -923 39) Perform each of the following operations on word-size 2’s complement numbers. For each, find the specified sum or difference. Determine whether overflow occurs. Depending on the operation, determine whether there is a carry or a borrow. Repeat the following operations in decimal. a. 8E3D + C3A9 b. 00C3 – 026A c. E9FF + 8CF0 11