Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10.

Similar presentations


Presentation on theme: "Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10."— Presentation transcript:

1 Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10

2 Today’s topics More MASM programming Addressing modes Conditional control structures Decision Repetition Debugging Review for Quiz #2

3 Instruction Operand Notation

4 IA-32 Operand Modes Immediate constant or literal, OFFSET (memory address) Examples: PIequ3.14159 sizeDWORD10 myNameBYTE ” Barney ”,0 moveax, 10 movedx, OFFSET myName Register register contents Examples: moveax, 10 addeax, ebx movsize, eax Direct memory contents Examples: moveax, size movsize, eax Others (later) Register indirect, Indexed, Base-indexed, Stack

5 SyntaxExamples MOV mem,imm mov color,7 mov response,’y’ SyntaxExamples MOV reg,imm mov ecx,256 mov edx,OFFSET string SyntaxExamples MOV mem,accum mov total,eax mov response,al MOV accum,mem mov al,char mov eax,size Notes: accum means “eax or some valid part of eax” imm means “a literal or constant”

6 SyntaxExamples MOV reg,reg mov dh,bh mov edx,ecx mov ebp,esp MOV mem,reg mov count,ecx mov num1,bx MOV reg,mem mov ebx,pointer mov al,response SyntaxExamples MOV sreg,reg16 mov ds, ax MOV sreg,mem16 mov es,pos1 MOV reg16,sreg mov ax,ds MOV mem16,sreg mov stack_save,ss Notes: mem8 means “BYTE” mem16 means “WORD” mem32 means “DWORD” sreg means CS, DS, ES, FS, GS, or SS

7 Invalid MOV statements.data bVal BYTE 100 bVal2 BYTE ? wVal WORD 2 dVal DWORD 5.code mov ds,45 mov esi,wVal mov eip,dVal mov 25,bVal mov bVal2,bVal immediate move to DS not permitted size mismatch EIP cannot be the destination immediate value cannot be destination memory-to-memory move not permitted

8 Branching execution Sometimes it is necessary to interrupt sequential instruction execution EIP is changed (but cannot be changed directly) Examples: Skip ahead (e.g., skip the else block) Jump backwards (e.g., repeat a section of code) Call a procedure Conditional / Unconditional branching Label required

9 MASM Labels Same rules as other identifiers May not be any previously defined identifier Label definition ends with colon : Don’t use : when referencing the label Specifies the memory address of the associated instruction … just like a variable name Good practice to put label: on a separate line.

10 Unconditional branching Instruction format is jmplabel label: should be inside the same procedure MASM allows jumps to labels in other procedures, but execution will certainly get lost in space.

11 Conditional branching Used for: if structures (decisions, alternation) loop structures (repetition, iteration) In general, MASM requires you to build your own control structures Note: in the following discussion, status bits (flags) are Set (means status bit is set to 1) Cleared (means status bit is set to 0)

12 loop instruction Instruction format is looplabel label: should be inside the same procedure, before the loop instruction Used for counted loops. Implements a “for” loop. Conditional branch Decrements ecx, if ecx is not zero, branch to label Problem if ecx is changed inside the loop body Problem if ecx starts at 0, or ecx becomes negative Exercise great care when constructing nested “loop” loops (there is only one ecx register !!)

13 loop Example Find sum of integers from 1 to 10 ; initialize accumulator, first number, ; and loop control moveax, 0 movebx, 1 movecx, 10 sumLoop:; add numbers from 1 to 10 addeax, ebx incebx loopsumLoop ; Print result callWriteDec ;...

14 Conditional branching We need a way to control branching by checking some other types of conditions Examples: Some repetitive tasks can not be counted in advance IF-THEN-ELSE structures MASM provides a way to compare two operands

15 CMP Instruction Compares the destination operand to the source operand Non-destructive subtraction of source from destination (destination operand is not changed) Sets specific bits in the status register Syntax: CMP destination, source

16 CMP Instruction (unsigned) Example: destination is equal to source mov al,5 cmp al,5; Zero flag set

17 CMP Instruction (unsigned) Example: destination < source mov al,4 cmp al,5; Carry flag set

18 CMP Instruction (unsigned) Example: destination > source mov al,6 cmp al,5; ZF = 0, CF = 0 (both the Zero and Carry flags are cleared)

19 CMP Instruction (signed) Example: destination > source mov al,5 cmp al,-2 ; Sign flag == Overflow flag The comparisons shown here are performed with signed integers. mov al,-1 cmp al,5 ; Sign flag != Overflow flag Example: destination < source

20 Conditional Jumps Jumps Based On... Specific flags Equality Unsigned comparisons Signed Comparisons

21 J cond Instruction A conditional jump instruction branches to a label when specific register or flag conditions are met Usually the next instruction after cmp Examples: JB, JC jump to a label if the Carry flag is set JE, JZ jump to a label if the Zero flag is set JS jumps to a label if the Sign flag is set JNE, JNZ jump to a label if the Zero flag is cleared JECXZ jumps to a label if ECX equals 0

22 Jumps Based on Specific Flags

23 Jumps Based on Equality

24 Jumps Based on Unsigned Comparisons

25 Jumps Based on Signed Comparisons

26 Compare and Jump cmp eax,ebx ja Larger Task: Jump to a label if unsigned EAX is greater than EBX Solution: Use CMP, followed by JA cmp eax,ebx jg Greater Task: Jump to a label if signed EAX is greater than EBX Solution: Use CMP, followed by JG

27 Compare and Jump cmp eax,Val1 jbe L1; below or equal Jump to label L1 if unsigned EAX is less than or equal to Val1 cmp eax,Val1 jle L1 Jump to label L1 if signed EAX is less than or equal to Val1

28 Conditional Loop Instructions (Use after CMP) LOOPZ and LOOPE Syntax: LOOPE destination LOOPZ destination Logic: ECX  ECX – 1 if ECX > 0 and ZF=1, jump to destination

29 Conditional Loop Instructions (Use after CMP) LOOPNZ and LOOPNE Syntax: LOOPNZ destination LOOPNE destination Logic: ECX  ECX – 1; if ECX > 0 and ZF=0, jump to destination

30 Conditional Directives.IF,.ELSE,.ELSEIF, and.ENDIF.WHILE,.ENDW.REPEAT,.UNTIL Not required for this course It’s OK to use these in programming assignments, but you must know the “hard way” for exams and quizzes

31 Block-Structured IF Statements You can create assembly language control structures that are equivalent to statements written in C++/Java/etc.. Example: mov eax,op1 cmp eax,op2 jne L1 mov X,1 jmp L2 L1: mov X,2 L2: if( op1 == op2 ) X = 1; else X = 2;

32 Assembly Language Control Structures Extend the concept to create your own: if-then if-then-else if-then-elseif-else compound conditions while loop do-while loop for loop nested structures, switch structures, etc.

33 if-then check condition using CMP if condition is false, jump to endThen (Note: test for complement of condition) code for TRUE block endThen:

34 if-then-else (Method 1) check condition using CMP if condition is false, jump to falseBlock (Note: test for complement of condition) code for TRUE block jump to endFalse falseBlock: code for FALSE block endFalse:

35 if-then-else (Method 2) check condition using CMP if condition is true, jump to trueBlock code for FALSE block jump to endTrue trueBlock: code for TRUE block endTrue:

36 if-then-elseif-else check condition1 using CMP if condition1 is true, jump to trueBlock1 check condition2 using CMP if condition2 is true, jump to trueBlock2 code for FALSE block jump to endBlock trueBlock1: code for TRUE block1 jump to endBlock trueBlock2: code for TRUE block2 endBlock:

37 Compound conditions (AND) check condition1 using CMP if condition1 is false, jump to falseBlock check condition2 using CMP if condition2 is false, jump to falseBlock code for TRUE block jump to endBlock falseBlock: code for FALSE block endBlock: NOTE: This structure implements short-circuit evaluation

38 Compound conditions (OR) check condition1 using CMP if condition1 is true, jump to trueBlock check condition2 using CMP if condition2 is true, jump to trueBlock code for FALSE block jump to endBlock trueBlock: code for TRUE block endBlock: NOTE: This structure implements short-circuit evaluation

39 Pretest loop (while) initialize loop control variable(s) top: check condition using CMP if condition is false, jump to endWhile code for LOOP BODY (including loop control update) jump to top (unconditional jump) endWhile:

40 Example pre-test loop Double x until x>1000 ; initialize accumulator moveax, x dblLoop:; Double x while x <= 1000 cmpeax, 1000 jaendLoop addeax, eax jmpdblLoop endLoop: movx, eax ;...

41 Posttest loop (do-while) top: code for LOOP BODY (including loop control update) check condition using CMP if condition is true, jump to top

42 Example post-test loop Double x until x>1000 ; initialize accumulator moveax, x dblLoop:; Double x while x <= 1000 addeax, eax cmpeax, 1000 jbedblLoop movx, eax ;... Note: may want initial test for x>1000

43 Various solutions Any control structure may be implemented in a variety of ways. Experiment!

44 Quiz #2 Wednesday in class Short answer, multiple-choice 40 minutes, 25 points

45 Quiz #2 Topics Intel IA-32 architecture Registers, memory addressing Bytes, words, etc. Little-endian representation Floating-point unit Continued … (next slide)

46 Quiz #2 Topics Emphasis on MASM General form of a MASM program Directives (TITLE, INCLUDE, PROC, ENDP, END) Segments (.code,.data) Declare variables, constants Comments Instruction format Instructions (mov, add, call, etc.) Trace MASM code sequential if-then, if-then-else, etc. for-loop, pre-test loop, post-test loop Convert simple statements to MASM Assembling, linking, loading, etc.

47 Questions? Quiz #2 Wednesday Programming Project #2 is posted Due Sunday, Feb 10 Learn the MASM instructions! Experiment! Experiment!! Experiment!!!


Download ppt "Wednesday Feb 1 Project #1 Due Sunday, February 3 Quiz #2 Wednesday, February 6, in class Programming Project #2 is posted Due Sunday, February 10."

Similar presentations


Ads by Google