汇编语言复习资料天津大学王建荣 联系客服

发布时间 : 星期五 文章汇编语言复习资料天津大学王建荣更新完毕开始阅读8a0ddc7f27284b73f2425022

5.Task: Jump to a label if unsigned EAX is greater than EBX Solution: Use CMP, followed by JA cmp eax,ebx ja Larger

6.Compare unsigned AX to BX, and copy the larger of the two into a variable named Large

mov Large,bx cmp ax,bx jna Next

mov Large,ax Next:

7.Compare signed AX to BX, and copy the smaller of the two into a variable named Small

mov Small,ax cmp bx,ax jnl Next

mov Small,bx Next:

8.Task: Jump to label L1 if bits 0, 1, and 3 in AL are all set. Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with 00001011 binary.

and al,00001011b ; clear unwanted bits cmp al,00001011b ; check remaining bits je L1 ; all set? jump to L1

9.Write code that jumps to label L1 if either bit 4, 5, or 6 is set in the BL register

and bl , 01110000b cmp bl , 0 jne L1

10.Write code that jumps to label L1 if bits 4, 5, and 6 are all set in the BL register

and bl , 01110000b cmp bl , 01110000b je L1

25

11.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 pushfd ; push flags on stack add esi,TYPE array

popfd ; loope L1 ; continue loop jz quit ; none found

sub esi,TYPE array ; ESI points to value quit:

(三)、高级语句和汇编语句的转化 1.

if( ebx <= ecx ) {

eax = 5; edx = 6; }

cmp ebx,ecx ja next mov eax,5 mov edx,6 next: 2.

if( var1 <= var2 ) var3 = 10; else {

var3 = 6; var4 = 7; }

26

; check for zero pop flags from stack

mov eax,var1 cmp eax,var2 jle L1 mov var3,6 mov var4,7 jmp L2

L1: mov var3,10 L2: 3.

if (al > bl) AND (bl > cl) X = 1;

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: 4.

if (al > bl) AND (bl > cl) X = 1;

cmp al,bl ; first expression... jbe next ; quit if false

cmp bl,cl ; second expression... jbe next ; quit if false mov X,1 ; both are true next: 5.

if( ebx <= ecx && ecx > edx ) {

eax = 5; edx = 6;

27

}

cmp ebx,ecx ja next cmp ecx,edx jbe next mov eax,5 mov edx,6 next: 6.

if (al > bl) OR (bl > cl) X = 1;

cmp al,bl ; is AL > BL? ja L1 ; yes

cmp bl,cl ; no: is BL > CL?

jbe next ; no: skip next statement L1: mov X,1 ; set X to 1 next: 7.

while( ebx <= val1) {

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

top: cmp ebx,val1 ; check loop condition ja next ; false? exit loop add ebx,5 ; body of loop dec val1

jmp top ; repeat the loop next:

(四)、习题6.5.5的参考答案

1.

cmp bx ,cx jna next move x ,1

28