简体   繁体   English

如何循环使用汇编语言

[英]How to loop in assembly language

How would I calculate the first 12 values in the Fibonacci number sequence and be able to place it in EAX reg. 我将如何计算斐波纳契数列中的前12个值并将其放置在EAX reg中。 and display calling DumpRegs? 并显示调用DumpRegs? Using Indirect addressing I know I need a for loop here but I'm not sure how to even go about this. 使用间接寻址,我知道我在这里需要一个for循环,但是我不确定该怎么做。 Any help or tips are appreciated. 任何帮助或提示,不胜感激。

      INCLUDE Irvine32.inc

; (insert symbol definitions here)

.data

; (insert variables here)
   Fibonacci BYTE 1, 1, 10 DUP (?)

.code
main PROC

; (insert executable instructions here)


    ; (This below will show hexa contents of string.)
      mov   esi, OFFSET Fibonacci       ; offset the variables
      mov   ebx,1                   ; byte format
      mov   ecx, SIZEOF Fibonacci       ; counter
      call  dumpMem 


    exit        ; exit to operating system
main ENDP

; (insert additional procedures here)

END main

You can make a loop like this: 您可以像这样进行循环:

mov ecx,12
your_label:
; your code
loop your_label

The loop instruction decrements ecx and jumps to the specified label unless ecx is equal to zero. 除非ecx等于零,否则循环指令将ecx递减并跳转到指定的标签。 You could also construct the same loop like this: 您也可以像这样构造相同的循环:

mov ecx,12
your_label:
; your code
dec ecx
jnz your_label

You determined that you need a for loop to achieve your goal, so maybe the C implementation of the for loop, in assembly, will help you: 您确定需要一个for循环才能实现自己的目标,因此,也许在汇编中使用for循环的C实现可以帮助您:

Code Generation for For Loop

for (i=0; i < 100; i++)
{
  . . .
}

      * Data Register D2 is used to implement i.
      * Set D2 to zero (i=0)
      CLR.L D2

  L1  
      . . .
      * Increment i for (i++)
      ADDQ.L #1, D2
      * Check for the for loop exit condition ( i < 100)
      CMPI.L #100, D2
      * Branch to the beginning of for loop if less than flag is set
      BLT.S L1

SOURCE: eventhelix.com 消息来源: eventhelix.com

.model small
.stack 100h
.data
   msg db 'Enter height of the square form 1-9: $'
   hash db '#$'
   height db 1
   length db 0
   ctr dw 0
   msgagain db 13,10,'Do you want to repeat the program? $'
   msgend db 13,10,'Program Terminated! Press any key to exit..$'
.code
   mov ax, @data
   mov ds, ax
REPEAT:   
   mov ax, 03
   int 10h

   mov ah, 09
   lea dx, msg
   int 21h

   mov ah, 01
   int 21h

   cmp al, '1'
   jl REPEAT
   cmp al, '9'
   jg REPEAT

   sub al, 48
   mov length, al
   mov bl, 1
   mul bl

   mov height, 1  
   mov di, 1 
   mov ctr, ax

   mov cx, ax
   nextline:
      dec length

      mov ah, 02
      mov bh, 00
      mov dl, length
      mov dh, height
      int 10h

      mov cx, di
      hashtags:
         mov ah, 09
         lea dx, hash
         int 21h
      loop hashtags      

      inc di
      inc height
      mov cx, ctr
      dec ctr
   loop nextline

   mov ah, 09
   lea dx, msgagain
   int 21h

   mov ah, 01
   int 21h

   cmp al, 'Y'
   je REPEAT
   cmp al, 'y'
   je REPEAT   

   mov ah, 09
   lea dx, msgend
   int 21h

   mov ah, 01
   int 21h

   mov ah, 4ch
   int 21h

END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM