Description
Computer Organization and Programming
Lab Work 11
Dr. Zulkar Nine mnine@gsu.edu
Learning Objective
Logical Instructions, and If & While statements
Disclaimer
Attendance!
Lab Work 11 Instructions
Lab 11 : Logical Instructions, and If & While statements
•
Plan early …
• You have one week time to submit the lab
• Start early
• If you have issues
• Start working at the last moment is not a good idea.
• AppendixA shows how to check memory data and Appendix B shows how to create a new project.
Problems in this lab
• You might see similar questions in the quizzes and exam.
• During the exam you might need solve similar problems without visual studio.
Compound Expression with AND (Example1)
•When implementing the logical AND operator, consider that HLLs use short-circuit evaluation
• In the following example, if the first expression is false, the second expression is skipped:
if (al <= bl) AND (bl >= cl)
X = 1;else
Y = 1
Compound Expression with AND (Example1)
•When implementing the logical AND operator, consider that HLLs use short-circuit evaluation
• In the following example, if the first expression is false, the second expression is skipped:
cmp al,bl ; first expression…
jbe L1
jmp Else_block L1: cmp bl,cl ; second expression…
jae If_block jmp Else_block
If_block: ; both are true mov X,1 ; set X to 1
jmp next Else_block:
mov Y, 1
Next:
if (al <= bl) AND (bl >= cl)
X = 1;else
Y = 1
WHILE Loops
• A WHILE loop tests a condition first before performing a block of statements.
• As long as the loop condition remains true, the statements are repeated.
• When implementing this structure in assembly language, o it is convenient to reverse the loop condition o and jump to endwhile if a condition becomes true
mov eax,val1 ; copy variable to EAX?
beginwhile: cmp eax,val2 ; if not (val1 < val2) jge endwhile ; exit the loop
inc eax ; val1++;
dec val2 ; val2–; jmp beginwhile ; repeat the loop
endwhile: mov val1,eax ; save new value for val1
Reverse The loop Condition
Example1
Example1
While ( (val1 != val2) AND (val1 > 0){
Val3++ }
Example1
While ( (val1 != val2) AND (val1 > 0){
mov eax,val1 ; copy variable to EAX?
beginwhile: endwhile: cmp eax,val2 ; if not (val1 == val2)
je endwhile cmp val1, 0
jbe endwhile
inc val3 ; val3++;
jmp beginwhile ; repeat the loop
Val3++ }
IF and While Statements
Example: Write and run a program to translate the following while and if statements:
WHILE Loops:
• IF statement Nested in a Loop
Shift and Rotate Instructions
Example: Write and run a program to find the values of each destination operand:
mov al,0D4h
shr al,1 mov al,0D4h ; a.
sar al,1 mov al,0D4h ; b.
sar al,4 mov al,0D4h ; c.
rol al,1 mov al,0D4h ; d.
ror al,3 ; e
Shift and Rotate: Instructions Shifting Multiple Doublewords
• Example: Write and run a program to find the values of each destination operand:
.data ArraySize = 3 array DWORD ArraySize DUP(99999999h) ; 1001 1001…
.code mov esi,0
shr array[esi + 8],1 ; high dword rcr array[esi + 4],1 ; middle dword, include Carry rcr array[esi],1 ; low dword, include Carry
Use the memory window to verify the result
Lab 11
Submission
Submission (1)
• Convert the following pseudo code into assembly code.
• Store the result in the variable – sum.
Submission Instruction
• Submit the screenshot of your code.
• Debug your code until you reach INVOKE ExitProcess, 0
• Take a screenshot of the watch window showing variable sum.
• Submit the screenshot.
• Also, Rename the asm file using your last name as Lastname.asm
• Submit the ASM file as well.
Appendix A
Checking Memory Data
• Use Memory window to verify the values of memory locations.
o To activate Memory window, run the debugger, go to debug menu and click on windows, open it, go to Memory then choose Memory1.
When you run your program and step over every line you will see the changed values marked with red color.
You Must be in the Debugging Mode to see the memory or the register window
o To activate Memory window,
if you want to see the location of your variable in the memory,
Memory window search box (on the top of the memory window, Address:) write & follow it with the variable name: example: &myVall.
This will take you to the memory locations of your program (.data section).
o To activate Memory window,
You can right-click inside the memory window
You will see Popup menu for the debugger’s memory window
You can choose how you want to group your bytes: by 1,2,4, or by 8
You can also presents data in hexadecimal, signed, or unsigned display
Appendix B
Create a Project
Step 1: Create a project (1)
(1) Start Visual Studio
(2) Click Create a new Project
Create a project (2)
(1) Select C++ as language
(2) Select Empty Project
(3) Click Next
Create a project (3)
(1) You can change the project name as you like
(1) Also, you can change the project location
(2) Click Next
Create a project (4)
Delete the
Following folders:
Header files
Resources Files, and
Source Files
Create a project (5)
To delete : Select the folders
Right click on it
Select delete
Create a project (6)
Select Project Name on solution explorer
Right click on it
Go to Build Dependencies
Click on Build Customizations
Create a project (7)
Select masm(.target, .props)
Click ok
Create a project (8)
Right click on the Project name in the solution explorer
Click properties
Create a project (9)
Expand the ‘Linker’
Select ’System’
Select Windows(/SUBSYSTEM:WINDOWS)
Click OK
Create a project (10)
Select Project name on solution explorer
Right click on it
Expand Add
Choose New Item
Create a project (11)
Select C++ File(.cpp)
Name: main.asm
Click Add
Create a project (12)
Select main.asm
Add your code
In the main.asm File.




Reviews
There are no reviews yet.