汇编流程控制

来源:互联网 发布:上学无用论 知乎 编辑:程序博客网 时间:2024/05/06 08:22
;**************************************************************
TITLE 取得三个数的最小值
;**************************************************************


;**************************************************************
;the process required platform
.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
;**************************************************************

;**************************************************************
;the file included
INCLUDE Irvine32.inc
;**************************************************************


;**************************************************************
;the data segment
.DATA
var_1 byte 2;
var_2 byte 74
var_3 byte 45;
;**************************************************************

;**************************************************************
;code segment
.CODE
;get the largest
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;首先比较var_1和var_2较大的放在al当中,然后和var_3比较
CMPproc proc
  mov al,var_1
  cmp al,var_2;
  ja M1;
  mov al,var_2  
  M1:;
  cmp al,var_3  
  ja M3;  
  mov al,var_3;
  M3:
  ret;
CMPproc endp
main proc
   xor eax,eax
   call CMPproc;
   call WriteInt
   exit
main endp
end main

;**************************************************************



;**********************************************************
TITLE 查找数组当中小于0的元素
;**********************************************************


;**********************************************************
;程序运行平台说明
.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
;**********************************************************

;**********************************************************
;文件包含
INCLUDE Irvine32.inc
;**********************************************************

;**********************************************************
;数据段定义
.DATA
filterByte byte 0,25,-87,24,24,45,0
arraySize byte $-filterByte
strEnter byte 0dh,0ah ,0
;**********************************************************


;**********************************************************
;代码段定义
.CODE

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;函数描述:从全局数组中filterByte寻找大于0的元素,输出
;返回值:无
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

FindAboveZero proc uses ecx edx eax
   movzx ecx,arraySize;
   xor eax,eax
   xor esi,esi
   FindLabel:
   mov al,filterByte[esi]
   cmp al,0
   js Continue;
   call WriteInt;
   mov edx,offset strEnter
   call WriteString
   Continue:
      inc esi   
   loopd FindLabel;   
   ret
FindAboveZero endp
main proc
  call FindAboveZero
  exit
main endp
end main
;**********************************************************

;******************************************************************
TITLE 找到数组中第一个不是0的数字
;******************************************************************

;******************************************************************
;程序运行平台
.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
;******************************************************************


;******************************************************************
;文件包含
INCLUDE Irvine32.inc
;******************************************************************


;******************************************************************
.DATA
arrayB byte 0,12,45,78,32,-89,0
arrayBSize byte $-arrayB
;******************************************************************



;******************************************************************
;代码段

.CODE
;------------------------------------------------------------------
;函数描述:寻找arrayB数组中第一个不为0的元素
;函数参数:ecx,esi
;返回值:无
FindFirstNon proc uses ecx esi
   mov ecx,length arrayB;
   xor esi,esi
   L1:
     mov al,arrayB[esi]
     test arrayB[esi],al
     pushfd
     add esi,type arrayB
     popfd
   loopnz L1;如果ecx>0并且
   jnz quit
   sub esi,type arrayB
   movzx eax,arrayB[esi];
   call WriteInt
   quit:
   ret;
FindFirstNon endp
;------------------------------------------------------------------
main proc
main endp
   call FindFirstNon
   exit
   ret;
end main
;******************************************************************

PS解读:

loopn 指令的含义是当ecx>0并且当标志寄存器zf==1时




;************************************************************
TITLE 表格的分支调用
;************************************************************


;************************************************************
;程序运行平台说明
.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
;************************************************************



;************************************************************
;文件包含
INCLUDE Irvine32.inc
;************************************************************

;************************************************************
;数据段定义
.DATA
msg1 byte "you called the process1",0dh,0ah,0
msg2 byte "you called the process2",0dh,0ah,0
msg3 byte "you called the process3",0dh,0ah,0
msg4 byte "you called the process4",0dh,0ah,0
msg5 byte "please input the 'A' 'B' 'C'  'D'",0dh,0ah,0
msgerror byte "you have input the wrong char",0dh,0ah,0
ProcessTable         byte 'A'
process_1            dword ?
                     byte 'B'
process_2             dword ?
                     byte 'C'
process_3             dword ?
                      byte 'D'
process_4             dword ?
;************************************************************


;************************************************************
;代码段
.CODE
process1 proc uses edx
   mov edx,offset msg1;
   call WriteString
   ret;
process1 endp


process2 proc uses edx
   mov edx,offset msg2;
   call WriteString
   ret;
process2 endp


process3 proc uses edx
   mov edx,offset msg3;
   call WriteString
   ret;
process3 endp


process4 proc uses edx
   mov edx,offset msg4
   call WriteString
   ret;
process4 endp
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;函数描述:根据用户输入的字符调用对应的函数
;函数参数:无
;函数返回值:无
FindRightFunction proc uses edx eax esi
    mov edx,offset msg5
    call WriteString
    ;读取的字符放在al当中,然后进行比较
    call ReadChar
    mov ecx,4;
    mov esi, offset ProcessTable;esi变化的步长是5
    L1:
        cmp [esi],al
        jz L2;
        jmp L3
        L2:
          inc esi;
          call dword ptr[esi]
          jmp L4;
        L3:
          add esi,5
    loopd L1;
    mov edx,offset msgerror
    call WriteString
    L4:
    ret;
FindRightFunction endp
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
main proc
   ;首先将各函数地址赋值给各变量
   mov process_1,process1;
   mov process_2,process2;
   mov process_3,process3
   mov process_4,process4  
   call FindRightFunction
   exit
main endp
end main
;************************************************************

原创粉丝点击