Use of loop instruction in assembly language

Source: Internet
Author: User


Now in the study of the Assembly, it feels very interesting. , I will write down the learning things, as a later can learn notes ~

The loop instruction is in the form of a loop designator, which takes two steps when the CPU executes the Loop command

1: (CX) = (CX)-1

2: Determine the value in CX, not zero to go to the label to execute, if zero, then execute down


From the description above, we can see the results of the execution of the impact loop instruction in CX, usually we use loop instruction to realize the loop function, the number of cycles stored in CX


Programming calculation 2^2, results stored in AX


Assembly code implementation, very simple

Assume Cs:code

Code segment

MOV ax,2

The. Ax, ax

MOV ax,4c00h

int 21h

Code ends

End

This implementation is simple, but if the power of a number of requirements is very large, it can not be so, just like in C language for the loop, with a loop to control the assembly language in the loop accumulation

Programming Computing 2^12

The code is as follows:

Assume Cs:code

Code segment

MOV ax,2

MOV cx,11

S:add Ax,ax

Loop s

MOV ax,4c00h

int 21h

Code ends

End

Analysis:

1: Marking, in assembly language, the label represents an address, this address has an instruction: add Ax,ax

In the above process, we can summarize the three key points to realize the loop function with CX and loop instructions.

(1) Number of cycles stored in CX

(2) The address identified by the label in the loop instruction should be in front

(3) The program segment to be executed is written in the middle of the label and loop instruction

The program framework for implementing the loop function with CX and loop directives is as follows

MOV cx, number of cycles

S

Loop-Executed program segments

Loop s


Okay, loop's fine.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.