《《计算机体系结构》第二次实验-指令集实验》由会员分享,可在线阅读,更多相关《《计算机体系结构》第二次实验-指令集实验(7页珍藏版)》请在金锄头文库上搜索。
1、指令集实验王宇航09283020指令集实验实验内容通过观察求素数程序在windlx以及Intel中的运行方式和内存中的存储方式,进行比较和分析。实验步骤1、 安装windlx模拟器,分析、运行其自带的求素数代码。详细分析其用到的各类机器指令格式。在windlx中将prim.s文件导入到其中并进行;-; Program begins at symbol main; generates a table with the first Count prime numbers from Table;-.data;* size of table.globalCountCount:.word10.globa
2、lTableTable:.spaceCount*4.text.globalmainmain:;* Initializationaddir1,r0,0;Index in Tableaddir2,r0,2 ;Current value/r1=r0+0 ; r2=r0+2 ;建立索引并初始化;* Determine, if R2 can be divided by a value in tableNextValue:addir3,r0,0 ;Helpindex in Table/r3=r0+0 ;建立帮助索引Loop:seqr4,r1,r3;End of Table?bnezr4,IsPrim;R2
3、 is a prime numberlwr5,Table(R3)divur6,r2,r5multur7,r6,r5subur8,r2,r7beqzr8,IsNoPrimaddir3,r3,4jLoop/循环判断是否为素数IsPrim: ;* Write value into Table and increment indexswTable(r1),r2addir1,r1,4;* Count reached?lwr9,Countsrlir10,r1,2sger11,r10,r9bnezr11,Finish/循环中的子函数,对是素数的进行存储和处理IsNoPrim:;* Check next va
4、lueaddir2,r2,1 ;increment R2jNextValue/循环中的子函数,对不是是素数的进行存储和处理Finish: ;* endtrap0/程序结束2、 编写c语言求素数程序,并分析其在Intel机器上执行的汇编代码。#include stdafx.h#include stdio.hint isprime(int n)int m=2;for(;m*m=n;+m)if(n%m=0)return 0;return 1;/循环中的子函数,对是素数的进行存储和处理,是素数则返回1,不是素数则返回0int main(int argc, char* argv)int b;for(b=
5、2;b=100;b+)if(isprime(b)printf(%dn,b);return 0;/主函数,利用循环对100以内的数进行判断并输出。以下为上面的程序翻译过来后的汇编程序。TITLEF:学习程序求素数求素数.cpp.386Pinclude listing.incif Version gt 510.model FLATelse_TEXTSEGMENT PARA USE32 PUBLIC CODE_TEXTENDS_DATASEGMENT DWORD USE32 PUBLIC DATA_DATAENDSCONSTSEGMENT DWORD USE32 PUBLIC CONSTCONSTE
6、NDS_BSSSEGMENT DWORD USE32 PUBLIC BSS_BSSENDS$SYMBOLSSEGMENT BYTE USE32 DEBSYM$SYMBOLSENDS$TYPESSEGMENT BYTE USE32 DEBTYP$TYPESENDS_TLSSEGMENT DWORD USE32 PUBLIC TLS_TLSENDS;COMDAT ?_C_03HMFC?$CFd?6?$AACONSTSEGMENT DWORD USE32 PUBLIC CONSTCONSTENDS;COMDAT ?isprimeYAHHZ_TEXTSEGMENT PARA USE32 PUBLIC
7、CODE_TEXTENDS;COMDAT _main_TEXTSEGMENT PARA USE32 PUBLIC CODE_TEXTENDSFLATGROUP _DATA, CONST, _BSSASSUMECS: FLAT, DS: FLAT, SS: FLATendifPUBLIC?isprimeYAHHZ; isprime;COMDAT ?isprimeYAHHZ_TEXTSEGMENT_n$ = 8_m$ = -4?isprimeYAHHZ PROC NEAR; isprime, COMDAT; 7 : pushebpmovebp, espsubesp, 68; 00000044Hpu
8、shebxpushesipushedileaedi, DWORD PTR ebp-68movecx, 17; 00000011Hmoveax, -858993460; ccccccccHrep stosd; 8 : int m=2;movDWORD PTR _m$ebp, 2; 9 : for(;m*m=n;+m)jmpSHORT $L530$L531:moveax, DWORD PTR _m$ebpaddeax, 1movDWORD PTR _m$ebp, eax$L530:movecx, DWORD PTR _m$ebpimulecx, DWORD PTR _m$ebpcmpecx, DW
9、ORD PTR _n$ebpjgSHORT $L532; 10 : if(n%m=0)moveax, DWORD PTR _n$ebpcdqidivDWORD PTR _m$ebptestedx, edxjneSHORT $L533; 11 : return 0;xoreax, eaxjmpSHORT $L528$L533:; 12 : return 1;jmpSHORT $L531$L532:moveax, 1$L528:; 13 : popedipopesipopebxmovesp, ebppopebpret0?isprimeYAHHZ ENDP; isprime_TEXTENDSPUBL
10、IC_mainPUBLIC?_C_03HMFC?$CFd?6?$AA; stringEXTRN_printf:NEAREXTRN_chkesp:NEAR;COMDAT ?_C_03HMFC?$CFd?6?$AA; File F:学习程序求素数求素数.cppCONSTSEGMENT?_C_03HMFC?$CFd?6?$AA DB %d, 0aH, 00H; stringCONSTENDS;COMDAT _main_TEXTSEGMENT_b$ = -4_mainPROC NEAR; COMDAT; 16 : pushebpmovebp, espsubesp, 68; 00000044Hpushe
11、bxpushesipushedileaedi, DWORD PTR ebp-68movecx, 17; 00000011Hmoveax, -858993460; ccccccccHrep stosd; 17 : int b; 18 : for(b=2;b=100;b+)movDWORD PTR _b$ebp, 2jmpSHORT $L539$L540:moveax, DWORD PTR _b$ebpaddeax, 1movDWORD PTR _b$ebp, eax$L539:cmpDWORD PTR _b$ebp, 100; 00000064HjgSHORT $L541; 20 : if(is
12、prime(b)movecx, DWORD PTR _b$ebppushecxcall?isprimeYAHHZ; isprimeaddesp, 4testeax, eaxjeSHORT $L542; 21 : printf(%dn,b);movedx, DWORD PTR _b$ebppushedxpushOFFSET FLAT:?_C_03HMFC?$CFd?6?$AA; stringcall_printfaddesp, 8$L542:; 22 : jmpSHORT $L540$L541:; 23 : return 0;xoreax, eax; 24 : popedipopesipopebxaddesp, 68; 00000044Hcmpebp, espcall_chkespmovesp, ebppopebpret0_mainENDP_TEXTENDSEND观察相同的素数在windlx内存以及Intel内存中存储的方式。素数在windlx内存以及Intel内存中存储的方式有所不同,DLX 指令格式 由于 DLX 的寻址方式较少,因此在指令格式中没有专门设置必要的寻址方式