[Assembly language notes] Six: Loop program structure

Past

 

[Assembly language notes] 0: (my first assembler)

[Assembly language notes] Two: data representation

[Assembly Language Notes] Three: Data Addressing

[Assembly language notes] Four: General data processing instructions

[Assembly language notes] Five: Sequential program structure

 

table of Contents

Loop program structure

Loop instruction

LOOP instruction

 JECXZ instruction

Array summation program

Count control loop

Overview:

Maximum seeking program

Conditional control loop

Overview:

Program for counting the number of strings ending in 0


Loop program structure

Loop instruction

LOOP instruction

LOOP instruction decrements by 1 and then judges

ECX = 0, loop 2 to the 32nd power

 JECXZ instruction

 

Array summation program

 

 

Register indexing to access array elements
          mov ebx, 0; point to the first element
aginn: add eax, arry [ebx * (type arry)]; sum
          add ebx, 1; point to the next array element

Register relative addressing to access array elements
          mov ebx, 0; point to the first element
aginn: add eax, arry [ebx]; sum
          add ebx, 4; point to the next array element

 Indirect addressing of registers to access array elements
          mov ebx, offset arry; point to the first element
aginn: add eax, [ebx]; sum
          add ebx, 4; point to the next array element

arry      dword 136,-138,133,130,-161    ;数组
sum       dword ?       ;结果变量
          mov ecx, legthof arry      ;ECX=数组元素的个数
          xor eax, eax       ;求和初值为0
          mov ebx, eax       ;数组指针为0
again:    add eax, arry[ebx*(type arry)]    ;求和
          inc ebx        ;指向下一个数组元素
          loop again
          mov sum,eax     ;保存结果       

Count control loop

Overview:

Maximum seeking program

        ;数据段
arry    dword -3,0,20,900,-56
count   = lengthof arry
max     dword ?

 1)

        ;代码段
        mov ecx, count-1
        xor esi, esi
        mov eax, arry[esi*(type arry)]
again:  add esi, 1
        cmp eax, arry[esi*(type arry)]  ;比较
        jge next           ;已是较大值,继续
        mov eax, arry[esi*(type arry)]
next:   loop again
        mov max, eax

2) 

        ;代码段
        mov ecx, count-1
        xor esi, esi
        mov eax, arry[esi*(type arry)]
again:  add esi, 1
        cmp eax, arry[esi*(type arry)]  ;比较
        jge next           ;已是较大值,继续
        mov eax, arry[esi*(type arry)]
next:   cmp esi, count-1
        jb again
        mov max, eax

Conditional control loop

Overview:

Program for counting the number of strings ending in 0

Judge first, recycle

        ;数据段
string  byte 'Do you have fun?',0
        ;代码段
        xor ebx, ebx
again:  mov al, string[ebx]
        cmp al, 0
        jz done     ;是0就跳转
        inc ebx
        jmp again
done:   mov eax, ebx
        call dispuid

Loop first, then judge

        ;数据段
string  byte 'Do you have fun?',0
        ;代码段
        xor ebx, -1
again:  inc ebx
        cmp string[ebx], 0
        jnz again
done:   mov eax, ebx
        call dispuid

References: Chinese University MoOC Platform Assembly Language Programming Course

Published 47 original articles · Like 23 · Visit 2677

Guess you like

Origin blog.csdn.net/weixin_43252204/article/details/105508909