Compilation nested loop

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/ma2595162349/article/details/90733224

          First look at a piece of code:

assume cs:codesg,ds:datasg,ss:stacksg
datasg segment
    db 'ibm             '
    db 'dec             '
    db 'dos             '
    db 'vax             '
datasg ends

stacksg segment
    dw 0,0,0,0,0,0,0,0
stacksg ends

codesg segment
    start:mov ax,stacksg
          mov ss,ax
          mov sp,16
          mov ax,datasg
          mov ds,ax
          mov bx,0

          mov cx,4
       s0:push cx     ;将外层循环的cx值压栈,在需要暂存数据时,一般选择栈
          mov si,0
          mov cx,3    ;设置内循环次数
        s:mov al,[bx+si]
          and al,11011111b  ;将字母变成大写
          mov [bx+si],al   ;变成大写重新赋值回去
          inc si          ;si自增,bx的值一直没变
          loop s

          add bx,16     ;程序的数据段是一块连续的内存,且bx一直为0,加上16指向下一个字符串
          pop cx      ;将栈顶的值传入寄存器cx中
          loop s0

          mov ax,4c00H
          int 21H
codesg ends
end start

     Action of the program is the segment of each word datasg uppercase letters, the length of each string is 16. Program to specify ss: sp, and ds: bx. Program [bx + si] may represent an address, said earlier [bx + idata], wherein idata is a constant, a register is now si, [bx + si + idata] represented by two variables and a constant a address, which is also permitted.

    Because the number of cycles within the set to use cx, cx therefore be saved with the outer loop stack up, and then the cycle is completed after the pop, pop cx after decrementing, then the stack push, thus completing the loop nest. Here to debug it.

   When the program is loaded, you can see the string and stack sections of the program

  After the program is running, you can see that the string has changed.

   

 

 

 

 

References: << >> Wang Shuang assembly language

Guess you like

Origin blog.csdn.net/ma2595162349/article/details/90733224