Download IAS2123-IID2163 Tutorial 2-Answer Scheme

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IAS2123 – Tutorial 2
ANSWER SCHEME
Tutorial 2
IAS2123: COMPUTER ORGANIZATION
Question 1
Use the following variable definition for the remaining questions in this section:
.data
MyByte SBYTE -10,-20,-11,11
MyWord WORD 300h,400h,500h,600h
MyDWord DWORD 11300h, 12400h,12500h,11600h
a)
For the following statements, state whether or not the instruction is valid. Give
comment to describe the reason why the instruction become valid or invalid.
i)
ii)
iii)
iv)
b)
mov ax, MyByte
mov ax, MyWord
mov eax, MyDWord
movzx ax, MyWord
= invalid
= valid
= valid
= invalid
What will be the value of the destination operand after each of the following
instructions executes in sequence? Write appropriate comment to explain the
operation executed by the instruction.
mov ax, MyWord
; i) Comment: mov MyWord to ax (16 bits)
; ax =300h
mov ax,[MyWord + 2]
; ii) Comment:Mov Myword+2 to ax (16 bits)
; ax =400h
add ax, [MyWord + 2]
; iii) Comment:Add value in ax(400h) with MyWord
+ 2(400h)
; ax =800h
1
IAS2123 – Tutorial 2
Question 2
a)
Use the following data for the next several questions:
.data
Val1 WORD 8000h
Val2 WORD FFFFh
Val3 WORD 7FFFh
Val4 BYTE 11001001b
i.
Write an instruction that transfer value of Val2 to register EAX.
mov eax, Val2
(1 marks)
ii.
Write an instruction to decrement value of Val1.
dec Val1
(1 marks)
iii.
Write the instruction to EXCHANGE val3 to val2.
Mov ax, val2
Xchg ax,val3
Mov val1,ax
(1 marks)
Question 3
a)
For the following statements, state either the instructions are valid or
invalid and state the reasons:
i)
ii)
xchg var1,var2
xchg var1,bx
error: two memory operands
; exchange mem, reg
Based on the following codes:
.data
bVal BYTE 100
bVal2 BYTE ?
wVal WORD 2
dVal DWORD 5
.code
iii)
iv)
v)
vi)
vii)
mov esi,wVal
mov eip,dVal
mov 25,bVal
movzx ax, bval
movsx al, wval
2
IAS2123 – Tutorial 2
b)
State the values of the destination operand after the following instructions
are executed in sequence. State the comments to explain the operations
executed by the instructions as well.
.data
arrayW WORD 1000h,2000h,3000h
arrayD DWORD 1,2,3,4
.code
i)
ii)
iii)
mov ax,[arrayW+2]
mov ax,[arrayW+4]
mov eax,[arrayD+4]
; AX = 2000h
; AX = 3000h
; EAX = 00000002h
Question 5
a)
Trace the error for the following assembly program. Rewrite and underline the correct
statement.
TITLE Add signed int, Version 2
(Addsigned.asm)
; This program adds and subtracts 32-bit integers
; and stores the sum in a variable.
; Last update: 30 dec 2007
val1
dword 100h
num1 sdword -10h
finalVal dword ?
main PROC
mov eax,val1
add eax,num1
mov finalVal,eax
; start with 10000h
; val1 add byte num1
; store the result (30000h)
exit
main ENDP
END main
(5 marks)
ANSWER
TITLE Add signed int, Version 2
(Addsigned.asm)
; This program momv and adds 32-bit integers
; and stores the sum in a variable.
; Last update: 30 dec 2007
INCLUDE Irvine32.inc
.data
3
IAS2123 – Tutorial 2
val1
num1
finalVal
dword
sdword
dword
100h
-10h
?
.code
main PROC
mov eax,val1
add eax,num1
mov finalVal,eax
Call DumpRegs
; start with 10000h
; val1 add byte num1
; store the result (30000h)
; display the registers
exit
main ENDP
END main
b)
Trace the output for EAX and EBX the following program.
INCLUDE Irvine32.inc
.data
val2 WORD 24h
val3 DWORD 11011b
val4 DWORD 8d
.code
main PROC
mov ax,val2
neg ax
movzx eax,ax
xchg eax,val3
mov ebx,val4
xchg ebx,val3
call DumpRegs
exit
;display the registers
main ENDP
END main
(5 marks)
4
IAS2123 – Tutorial 2
Question 6
Write a complete program in assembly language that implements the following arithmetic
expression:
EAX = -val2 * 7 + val3
In comments next to each instruction, write the hexadecimal value of EAX. Insert a call
DumpRegs statement at the end of the program.
Use the following data definition in your program:
.data
val2 SWORD -11000h
val3 SWORD 21000h
5
IAS2123 – Tutorial 2
TITLE ARITHMETICS
;This program implement arithmetic expression in assembly language
(1 marks)
INCLUDE Irvine32.inc
.data
val2 SDWORD -11000h
val3 SDWORD 21000h
.code
main PROC
;EAX = -val2 * 7 + val3
mov eax,val2 ; eax = -11000h
neg eax
; eax = 11000h
mov ebx, 7
mul ebx
; ebx = 7
; 11000h*7=77000h
mov ebx,val3 ; ebx=21000h
add eax,ebx ; eax=98000h
call DumpRegs
exit
main ENDP
END main
Question 7
Write a complete program using MASM Assembly Language 32-bit integers with
different registers. Insert the call DumpRegs statement to display the registers and flags.
Apply the following arithmetic expression and data given.
answer = (-val2 + val3) * val1
Use the following data:
val1 = 150h
val2 = Ah
val3 = 26d
(15 marks)
ANSWER
;EAX = (-val2 + val3) * val1
6
IAS2123 – Tutorial 2
INCLUDE Irvine32.inc
.data
val1 DWORD 150h
val2 DWORD 0Ah
val3 DWORD 26d
answer DWORD ?
.code
main PROC
mov ebx,val2
mov ecx,val3
neg ebx
add ebx,ecx
mov eax,val1
mul ebx
mov answer,ebx
call DumpRegs
;ebx = Ah
;ecx = 26d = 1Ah
;ebx = -Ah =FFFFFFF6h
;ebx = -Ah+1Ah =10
;eax = 150h
;ebx =10*150
;eax = 1500h
;display the registers
exit
main ENDP
END main
7