cet 3510 - microcomputer systems technology lecture 5

27
CET 3510 - Microcomputer Systems Technology Lecture 5 Dr. José M. Reyes Álamo 1

Upload: rupali

Post on 23-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

CET 3510 - Microcomputer Systems Technology Lecture 5. Dr. José M. Reyes Álamo. Outline. Review: of Comparisons of Set on Condition Statement Labels Unconditional Jumps Conditional Jumps. Review of Comparison. HLA Syntax: cmp (Left, Right) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CET 3510 - Microcomputer Systems Technology Lecture 5

CET 3510 - Microcomputer Systems Technology

Lecture 5Dr. José M. Reyes Álamo

1

Page 2: CET 3510 - Microcomputer Systems Technology Lecture 5

Review:◦ of Comparisons◦ of Set on Condition

Statement Labels Unconditional Jumps Conditional Jumps

Outline

Page 3: CET 3510 - Microcomputer Systems Technology Lecture 5

HLA Syntax: cmp(Left, Right) Used for comparisons, same as sub, but instead

of returning the result only sets certain bits in the flags register.◦ Z: The zero flag is set if and only if Left = Right. ◦ S:The sign flag is set to one if the result is

negative. ◦ O: The overflow flag is set if the difference of Left

and Right produced an overflow or underflow.◦ C:The carry flag is set if subtracting Right from

Left requires a borrow.

Review of Comparison

Page 4: CET 3510 - Microcomputer Systems Technology Lecture 5

Review of Comparison Syntax

Page 5: CET 3510 - Microcomputer Systems Technology Lecture 5

Comparison Interpretation

Page 6: CET 3510 - Microcomputer Systems Technology Lecture 5

The set on condition (or setcc) instructions set a single byte operand (register or memory location) to zero or one depending on the values in the flags register

These instructions store a zero into the operand if the condition is false, a one if the condition is true

Useful for mapping the result of a comparison to a Boolean value

Review on Set on Condition

Page 7: CET 3510 - Microcomputer Systems Technology Lecture 5

Review Set on Condition Syntax

Page 8: CET 3510 - Microcomputer Systems Technology Lecture 5

Review Set on Condition Syntax

Page 9: CET 3510 - Microcomputer Systems Technology Lecture 5

Review Set on Condition Syntax

Page 10: CET 3510 - Microcomputer Systems Technology Lecture 5

HLA control structures are similar to C++ and other high-level language

These are NOT true assembly language Now you will learn how these are

represented in real assembly

Low Level Control Structures

Page 11: CET 3510 - Microcomputer Systems Technology Lecture 5

A low level control structure usually transfers control from one point in your program to another.

Transfer destination is typically specified of using a statement label.

A statement label consists of a valid HLA identifier and a colon.

Don’t have to be declared before you use it SyntaxaLabel:

Statement Labels

Page 12: CET 3510 - Microcomputer Systems Technology Lecture 5

1. Transfer control to a label via a jump (goto) instruction

2. Call a label via the CALL instruction, 3. Take the address of a label

◦ Use the address-of operator &◦ Use the command load effective address leaSyntax:

lea( reg32, Memory_operand );

Operations with Labels

Page 13: CET 3510 - Microcomputer Systems Technology Lecture 5

Obtaining the Address of a Labelprogram labelDemo; #include( "stdlib.hhf" );begin labelDemo; lbl1: lea( ebx, lbl1 ); stdout.put( "&lbl1=$", ebx, " &lbl2=", &lbl2,

nl ); lbl2:end labelDemo;

Page 14: CET 3510 - Microcomputer Systems Technology Lecture 5

The jmp (jump) instruction unconditionally transfers control to another point in the program.

There are three forms of this instruction one direct jump, and two indirect in the following forms:◦ jmp label;◦ jmp( reg32 );◦ jmp( mem32 );

Unconditional Jump (JMP)

Page 15: CET 3510 - Microcomputer Systems Technology Lecture 5

Direct jump specifies the target using a label The label is usually on the same line as an executable

instruction or appears on a line preceding an executable machine instruction.

The direct jump instruction is the most commonly used.

Equivalent to a GOTO statement Syntax:

…jmp laterInPgm;…laterInPgm:…

Direct Jump

Page 16: CET 3510 - Microcomputer Systems Technology Lecture 5

Transfers control to the instruction whose address is specified in the 32-bit general purpose register.

You must load the specified register with the address of some machine instruction prior to the execution of the JMP.

Syntax:… mov(&laterInPgm, ebx);jmp (ebx);…laterInPgm:…

Register Indirect Jump

Page 17: CET 3510 - Microcomputer Systems Technology Lecture 5

Memory indirect fetches a dword value from the specified memory location and transfers control to the instruction at the address specified by the contents of the memory location.

Similar to the register indirect JMP except the address appears in a memory location rather than in a register.

Syntax:<< statements >>LabelPtr:dword := &stmtLabel;…jmp (LabelPtr);…stmtLabel :…

Memory Indirect Jump

Page 18: CET 3510 - Microcomputer Systems Technology Lecture 5

Low-level JMP instructions can get you into a lot of trouble.

If you do not initialize a register with the address of a valid instruction and you jump indirect through that register, the results are undefined.

If you do not initialize a dword variable with the address of a legal instruction, jumping indirect through that memory location will probably crash your program.

Warning!!!

Page 19: CET 3510 - Microcomputer Systems Technology Lecture 5

Unconditional jmp provides transfer of control but does not allow you to make decisions.

Conditional jumps handle this task. Conditional jumps are the basic tool for

creating loops (i.e. while, for, repeat) and other conditionally executable statements (i.e. if, switch)

Syntax jcc label;cc indicated the type of test you are performing.

Conditional Jump

Page 20: CET 3510 - Microcomputer Systems Technology Lecture 5

The conditional jumps test one or more flags in the EFLAGS register to see if they match a particular pattern.

If the flags match, the control jumps to the target label.

If the match fails, the CPU ignores the conditional jump and execution continues with the next instruction.

Most of the time, conditional jumps follow the execution of a cmp instruction as the cmp sets the EFLAGS register allowing tests for less than, greater than, equality, etc.

Conditional Jump Tests

Page 21: CET 3510 - Microcomputer Systems Technology Lecture 5

Conditional jump instructions do not provide an indirect form, only allow is a jump to a label in your program.

Conditional jump target label must be within 32,768 bytes of the jump instruction.

This generally corresponds to somewhere between 8,000 and 32,000 machine instructions, it is unlikely you will ever encounter this restriction.

Conditional Jump Limitations

Page 22: CET 3510 - Microcomputer Systems Technology Lecture 5

Types of Conditional Jump

Page 23: CET 3510 - Microcomputer Systems Technology Lecture 5

Types of Conditional Jump

Page 24: CET 3510 - Microcomputer Systems Technology Lecture 5

Types of Conditional Jump

Page 25: CET 3510 - Microcomputer Systems Technology Lecture 5

In many instances you will need to generate the opposite of a specific jump instruction.◦ If the second letter of the jcc instruction is not an

“n”, insert an “n” after the “j”. (e.g., je becomes jne and jl becomes jnl)

◦ If the second letter of the jcc instruction is an “n”, then remove that “n” from the instruction. (e.g., jng becomes jg and jne becomes je.

◦ Exception: jpe (jump if parity is even) vs. jpo (jump if parity is odd).

Conditional Jump Tips

Page 26: CET 3510 - Microcomputer Systems Technology Lecture 5

Example of an decision statement

C++ HLA

if (x == y)a++;

mov(x, bx)mov(y, cx)cmp( bx, cx );jne SkipStmts;

inc( ax );SkipStmts:

Page 27: CET 3510 - Microcomputer Systems Technology Lecture 5

Example of a loop statement

C++ HLA

mov (0, eax);mov (100, ebx);

WhileLabel:cmp(eax, ebx); jnl WhileDone;

inc(eax); jmp WhileLabel;

WhileDone:

C++

x = 0;while(x <= 100){

x++;}