1 / 44

Quiz #2 Topics

Quiz #2 Topics. Character codes Intel IA-32 architecture Registers, memory addressing Bytes, words, etc. Little-endian representation Floating-point unit Mostly MASM General form of a MASM program Directives (TITLE, INCLUDE, etc.) Segments (.code, .data, etc.)

salali
Download Presentation

Quiz #2 Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Quiz #2 Topics Character codes Intel IA-32 architecture Registers, memory addressing Bytes, words, etc. Little-endian representation Floating-point unit Mostly MASM General form of a MASM program Directives (TITLE, INCLUDE, etc.) Segments (.code, .data, etc.) Declare variables, constants Comments Instruction format Instructions (mov, add, call, etc.) Trace MASM code Convert simple statements to MASM Assembling, linking, loading, etc.

  2. Today’s topics More MASM programming Addressing modes Conditional control structures Decision Repetition

  3. IA-32 Operand Modes Immediateconstant or literal, OFFSET (memory address) Examples: PI equ 3.14159 size DWORD 10 myName BYTE ”Barney” mov eax, 10 mov edx, OFFSET myName Registerregister contents Examples: mov eax, 10 add eax, ebx mov size, eax Directmemory contents Examples: mov eax, size mov size,eax Others (later) Register indirect, Indexed, Base-indexed, Stack

  4. Instruction Operand Notation

  5. Notes: accum means “eax or some valid part of eax” imm means “a literal or constant”

  6. 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 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 : 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 jmp label 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 loop label 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

  13. loop ExampleFind sum of integers from 1 to 10 ; initialize accumulator, first number, ; and loop control mov eax, 0 mov ebx, 1 mov ecx, 10 sumLoop: ; add numbers from 1 to 10 add eax, ebx inc ebx loop sumLoop ; Print result call WriteDec ; ...

  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) 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 The comparisons shown here are performed with signed integers. mov al,5 cmp al,-2 ; Sign flag == Overflow flag • Example: destination < source mov al,-1 cmp al,5 ; Sign flag != Overflow flag

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

  21. Jcond 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 • Task: Jump to a label if unsigned EAX is greater than EBX • Solution: Use CMP, followed by JA cmp eax,ebx ja Larger • Task: Jump to a label if signed EAX is greater than EBX • Solution: Use CMP, followed by JG cmp eax,ebx jg Greater

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

  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 idea 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:

  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:

  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 endWhile:

  40. Example pre-test loopDouble x until x>1000 ; initialize accumulator mov eax, x dblLoop: ; Double x while x <= 1000 cmp eax, 1000 ja endLoop add eax, eax jmp dblLoop endLoop: mov x, 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 loopDouble x until x>1000 ; initialize accumulator mov eax, x dblLoop: ; Double x while x <= 1000 add eax, eax cmp eax, 1000 jbe dblLoop mov x, 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 Thursday Program #2 is assigned Learn the MASM instructions! Experiment! Experiment!! Experiment!!! Questions?

More Related