0% found this document useful (0 votes)
11 views

Lab 5

Uploaded by

kingorijoseph180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab 5

Uploaded by

kingorijoseph180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Conditional Loop Instructions

Example 1

The following code finds the first positive value in an array:


.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here

array SWORD -3,-6,-1,-10,10,30,40,4


sentinel SWORD 0

.code

main PROC
; write your assembly code here

mov esi,OFFSET array


mov ecx,LENGTHOF array
next:
test WORD PTR [esi], 8000h ; test sign bit
pushfd ; push flags on stack
add esi, TYPE array
popfd ; pop flags from stack
loopnz next ; continue loop
jnz quit ; none found
sub esi, TYPE array ; ESI points to value
quit:

INVOKE ExitProcess, 0
main ENDP
END main

Example 2

Locate the first nonzero value in the array. If none is found, let ESI point to the sentinel value:
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0 ; check for zero
(fill in your code here)

quit:
Solution

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD

.data
; define your variables here

array SWORD 50 DUP(?)


sentinel SWORD 0FFFFh

.code

main PROC
; write your assembly code here

mov esi,OFFSET array


mov ecx,LENGTHOF array

L1: cmp WORD PTR [esi], 0 ; che


check for zero
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loope L1 ; continue loop
jz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:

INVOKE ExitProcess, 0
main ENDP
END main

Conditional Structures

Example 3

Assembly language programmers can easily translate logical statements written in C++/Java into
assembly language.

For example:

if( op1 == op2 )

X = 1;

else

X = 2;
Example 4

Implement the following pseudocode in assembly language. All values are unsigned:

(There are multiple correct solutions to this problem.)

Example 5

Implement the following pseudocode in assembly language. All values are 32


32-bit signed integers:

(There are multiple correct solutions to this problem.)

Example 5: 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 b1, c1 ;second expression
ja L2
jmp next

L2: ; both are true


Mov X, 1 ; set X to 1

But the following implementation uses 29% less code by reversing the first relational operator. We
allow the program to “fall
fall through" to the second expression:

if (al > bl) AND (bl > cl)


X = 1;

next:

Example 6:

Implement the following pseudocode in assembly language. All values are unsigned:
(There are multiple correct solutions to this problem.)

Example 7: Compound Expression with OR

Example 8: WHILE Loops

A WHILE loop is really an IF statement followed by the body of the loop, followed by an unconditional
jump to the top of the loop. Consider the following example:

while( eax < ebx)


eax = eax + 1;

next:
Example 9: Implement the following loop, using unsigned 32
32-bit integers:

while( ebx <= val1)


{
ebx = ebx + 5;
val1 = val1 - 1
}

next:

Example 10: IF statement nested in a loop


_while:

else:

_endwhile:

You might also like