Assembly Language & System Software 99 Spring National Chiao-Tung University Time: 120 minutes Instructions: A. There are FIVE pages. B. There are 13 questions. You must answer all of them. C. Write your answers on the answer book ONLY. D. DO NOT TURN OVER the cover page until you are told to do so. E. CHEATING IS A SERIOUS MATTER. YOU WILL RECEIVE A SCORE OF ZERO and SERIOUS PENALTY IF YOU ARE CHEATING. 1. Estimation: Questions 1 to 4: 10 minutes. [ 4%] Write down the value (hexadecimal format) of AX after each of the following code segments is executed. (a) (b) (c) (d) mov ax, 10h mov ax, 100 mov ax, 8 mov ax, 0f9h or ax, 7h and ax, 1dh xor ax, 7 test ax, 1 2. [ 4%] Write down the value (hexadecimal format) of AX after each of the following code segments is executed. Note: AX is a part of EAX. (a) (b) (c) (d) mov eax, 11h mov eax, 99 mov eax, 0ffedh mov eax, 0fah shl eax, 2 shr eax, 16 sal eax, 4 ror eax, 4 3. [2%] (a) What is the value of ABC? Your answer should be in decimal format. symbols WORD 1, 2, 3, 21h, 22h, 23h, 40h, 41, 42, 43 ABC = ($ - symbols) [1%] (b) What does the directive “.data?“ indicate ? 4. [1%] (c) Give one example. [6%] (each cell 0.25%) Draw and fill out the cells for memory based on the following declarations. Each cell represents one byte. Little endian is used for storing integers. All numbers are in hexadecimal format. V0 BYTE 12h, 13h, 14h BYTE “test”, 0 V1 WORD 1024, 1025, 1026 V2 SWORD -1, -2, -3 V3 DWORD 98765432h Remark: you must follow the sample of the layout as follows (memory increasing from top to bottom): 0 1 2 3 4 5 6 7 Estimation: Questions 5 to 6: 10 minutes. 5. [2%] (a) Write at most three lines of code to combine ax:dx as a single value into eax. Hint: the high order part of eax is the content of the initial value of ax. [2%] (b) Write two lines of code to compute the sum of ax, bx and cx and the result is stored in ax. [1%] (c) When is zero flag set to one? [1%] (d) When is overflow flag set to one? [5%] (e) In the following instruction sequence, show the values of the Zero flag (ZF) and overflow flag (OF) where they appears: L1: mov L2: test L3: mov L4: and L5: mov L6: add L7: mov L8: sub L9:jmp 6. al, al, al, al, al, al, al, al, L1 00001111b 10h 0bh 5 00001101b 11110011b 10000001b 01111111b ; ZF= ?; OF = ? ; ZF= ?; OF = ? ; ZF= ?; OF = ? ; ZF= ?; OF = ? ; ZF= ?; OF = ? The following fragment computes a value and the value is stored in EDX. [5%] (a) What does the fragment do? Describe in words. Assume that all the values can be stored in a 32-bit register. [2%] (b) What is the value stored in EDX after the fragment is executed? Your answer should be in hexadecimal format. mov ax, 1 xor edx, edx mov ecx, 5 L1: mov bx, ax push ax push edx mul bx pop edx add edx, eax pop ax inc ax Loop L1 Estimation: Questions 7 to 10: 40 minutes. 7. [8%] The following fragment computes the sum of the signed integers of an array ARRAY and then displays the sum on the screen. The array has at least one element. The total size, i.e. number of bytes, of the array is stored in ECX. A signed integer is represented by FOUR bytes. Fix the syntax or semantic error for each line. You must not change the structure of the fragment. .data L1: ARRAY WORD 10000 DUP(?) ; reserve space for the array ...... L2: mov edi, ARRAY ; store the address of ARRAY to edi L3: shr ecx, 1 ; compute the number of integers in the array L4: xor ax, ax ; L5: add ax, DWORD PTR [edi] L6: add edi, TYPE NEXT L7: loop L4 L8: call WriteDec 8. [4%] EAX and EBX store integers. For different types of integers, we need different instructions for determining the jumping branches after a comparison is performed. Explain the purpose of each conditional jump: (a) cmp eax, ebx (b) cmp eax, ebx (c) cmp eax, ebx (d) cmp eax, ebx ja L1 jge L1 jnl L1 jnbe 9. [7%] Suppose that we have five SDWORDs, a, b, c, d, and e. Write assembly instructions to compute: eax = (a-b)*(-e) / (d – b + a) Note that you must use no more than 7 instructions. Assume that all the numbers (including the ones from computation) can be stored in a 32-bit register. -1pts per extra instruction. 10. Translate each of the following C++ functions into a function written in assembly language: [5%] (a) unsigned int computeTaskOne ( unsigned int a ) { cout << a << endl; //Hint: endl should be handled. return a + a; } The result should be stored in EAX. [8%] (b) void computeTaskThree ( int n, int *arr, int *brr ) { if ( n <= 0 ) return; for ( int i = 0; i < n; ++i ) { arr[i] = brr[i] * brr[i] – 1; } } Estimation: Questions 11 to 13: 40 minutes. 11. A one-dimensional array is used for representing a 2D array which is organized as row-major order. The numbers of rows and columns are stored in eax, and ebx, respectively. The starting address of the array is stored in edi. An element of the array can be represented as (r, c), where r and c are the row index and column index of the element, respectively. The index starts from 0. The data type of elements is DWORD. Example: write one line of code to store the element (1, 2) to ecx. mov [edi+ebx*4+2*4], ecx [1%] (a) write one line of code to store the element (0, 0) to ecx. [1%] (b) write one line of code to store the element (0, ebx-1) to ecx [4%] (c) write at most four lines of code to store the element (ecx, edx) to esi [8%] (d) write at most 8 instructions to compute the sum of the edx –th row to ebx. -1pts per extra instruction. Hint: Draw a EAX x EBX two dimensional array for easy calculation. 12. [12%] Translate the SIC/XE program into object code (a) – (l). Register Code: S : 4; T: 5; X : 1; Location Object code (Hints) --------------------------------------------------------FINAL START 0 (La)0000 +LDS LENGTH (a) (format 4) (Lb)0004 STL RETADR (b) (PC-relative) (Lc)0007 CLEAR S (c) (Ld)0009 CLEAR X (d) (Le)000B LDB #LENGTH (e) (imme. addr) (Lf)000E CMPR X, T (f) (format 2) (Lg)0010 JGT EXIT (g) (PC-relative) (Lh)0013 COMP #0 (h) (imme. addr) (Li)0016 JEQ EXIT (i) (PC-relative) (Lj)0019 ADDR S, X (j) (Lk)001B LDL RETADR (k) (PC-relative) (Ll)001E EXIT RSUB (l) (format 3) (Lm)0021 LENGTH WORD 41 (Ln)0024 RETADR RESW 2 13. [6%] A SIC/XE program is given as follows: Location Object code --------------------------------------------------------OBJP START 1012 1012 CLEAR B B430 1014 CLEAR T B450 1016 CLEAR F B460 1018 RETADR RESW 2 no object code 101E DATA RESW 2 no object code 1024 LENGTH WORD 32 20 00 00 Translate the SIC/XE program to an object program. You can use at most TWO text records. Hint: make sure the number of columns is correct. A column represents a half-byte (4-bit).