3 Solution Assignment - 02 Coal
3 Solution Assignment - 02 Coal
3 Solution Assignment - 02 Coal
Assignment # 02
Q#1:
Explain Conditional Loop Instructions in assembly language with the help of examples?
Ans#1
Conditional Loop Instructions:
• Syntax: LOOPE destination LOOPZ destination
• Logic: • ECX ← ECX – 1 • if ECX > 0 and ZF=1, jump to destination.
• Useful when scanning an array for the first element that does not match a given value.
Conditional Structures:
• Block-Structured IF Statements
• Compound Expressions with AND
• Compound Expressions with OR
• WHILE Loops
• Table-Driven Selection
Block -Structured IF Statements:
Assembly language programmers can easily translate logical statements written in C++/Java into
assembly language. For example:
C++:
if( op1 == op2 )
X = 1;
else X = 2;
Assembly language:
mov eax,op1
cmp eax,op2
jne L1
mov X,1
jmp L2
L1:mov X,2
L2:
Compound Expression with AND:
• 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;
Compound Expression with AND:
if (al > bl) AND (bl > cl)
X = 1;
This is one possible implementation:
cmp al,bl ; first expression…
ja L1
jmp next
L1: cmp bl,cl ; second expression...
ja L2
jmp next
L2: ; both are true
mov X,1 ; set X to 1
next:
where 'value' is the label field, 'EQU' is the operation field, '$AA' is the operand, and the comment
section at the end begins with a semi-colon.
Directives are instructions used by the assembler to help automate the assembly process and to
improve program readability. Examples of common assembler directives are ORG (origin), EQU
(equate), and DS.B (define space for a byte). Directives are NOT part of the Motorola assembly
language machine instructions, such as 'ldaa' or 'movb'. Machine instructions generate machine
code, assembler directives do not. Directives are used essentially in a pre-processing stage of the
assembly process.
Essential Code Warrior Syntax
Note that while directives are not case sensitive, labels and symbols ARE case-sensitive: 'Buf1:' !=
'buf1:' != 'BUF1:', etc.
* Comments - A comment is used to explain the purpose and usage of a block of statements
or to describe an algorithm. A comment line contains a semicolon followed by text. A comment
can also be placed at the end of a source line. Comments are included in the assembly listing, but
are not significant to the assembler.
Examples:
ldaa#$01; put $01 into reg A -> comment at end of source line
string1:dc.b'hello'; this creates a string 'hello' whose storage address begins at string1.
You can think of 'num1' as an address or as the contents of that address, depending on the
context of the code. For instance, imagine that after assembling the program, we find that num1 is
assigned to memory address $2000 (you could find this our by disassembling the code using the
'asm' command, for example). The contents of address $2000 is the 8 bit number $FC. If you write
'ldaa num1', we would put the contents of the address $2000 into reg A, which is the same as saying
we would put the value $FC into reg A. So, writing 'ldaa num1' is like a synonym for 'ldaa #$FC'.
If we wrote 'ldd #num1', this would put the address $2000 into reg D; this is like a synonym for 'ldd
#$2000'. Note that if you tried to use 'ldaa #num1', you would get a warning during assembly since
you are trying to put a 16 bit number (the address $2000) into an 8 bit register, reg A. Reg D is 16
bits so we don't get that warning. Similar results apply to the use of the dc.w, ds.b and ds.w
directives noted below. Note the similarity to pointers in C.
* DC.W - Define constant in memory of length one word (two bytes). Two bytes are
allocated for numeric expressions and are assigned to address M:M+1. ASCII strings are right
aligned on a two-byte boundary.
Example:
k1:dc.w$90AE; k1 has address M:M=+1 and is initialized to $90AE
* EQU - The equate directive is used to substitute values for symbols or labels. The format
is 'label: EQU value', so whenever the assembler encounters 'label', it replaces this with 'value'. This
is especially handy for improving program readability. For instance, we could write 'ldx #$0032',
or we could use 'portb: EQU $0032' followed later by 'ldx #portb'. The latter 'ldx' instruction is
more readable than the former. The EQU directive only tells the assembler to substitute a value for
a symbol or label, and doesn't involve any type of ROM or RAM. EQU directives are typically
placed at the beginning of an assembly program.
Examples:
portb:EQU$0032; port B is located at address $0032
portb_ddrEQUportb + 2; port B Data Direction Register = $0034
kb:EQU$1024; kb = 1024 bytes
* OFFSET - The OFFSET directive declares an offset section and initializes the location
counter to the value specified in expression following offset. An offset section is useful to simulate
data structures or a stack frame.
Example:
OFFSET 0
ID: DS.B 1
COUNT: DS.W 1
VALUE: DS.L 1
SIZE: EQU *
* ORG - The origin directive sets the location counter to the value specified. Subsequent
statements are assigned memory locations starting with the new location counter value. The location
counter is a counter in the assembler program that is used to assign storage addresses for the
program. For instance, it will set the location in memory where the constants are stored, and where
the executable machine code begins. As the program is assembled, the location counter keeps track
of the current location in storage. Note that the location counter is a software construct defined in
the assembler program, and exists only during the assembly process. It is NOT the same as the
program counter, which is an actual register in the HCS12 that keeps track of the address of the
current instruction as an executable is running on the HCS12. Note that unlike labels, ORG must
be indented (it must have whitespace in the leading columns).
* SET - Similar to the EQU directive, the SET directive assigns the value to the symbol.
Unlike EQU, however, the label associated with SET can be redefined.
Examples:
c1:SET$12; 'c1' substitutes for $12
c1:SET$18; 'c1' substitutes for $18 - couldn't redefine c1 if EQU was used
* XDEF - Make a symbol public (needed by the linker to find out where the entry point is).
This is required for all of our programs, and is usually the first line following the header comments.
As with the ORG directive, XDEF must be indented (it must have whitespace in the leading
columns).
Example:
XDEFEntry; export the Entry symbol, which is a label at the start of our program