您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > nachos实验一代码
Test文件下:shell.c:#includesyscall.hintmain(){SpaceIdnewProc;OpenFileIdinput=ConsoleInput;OpenFileIdoutput=ConsoleOutput;charch,buffer[60];inti;Write(\t\t-ZhangCJ'sshellwelcomeyou-\n,34,output);while(1){Write(-PleaseInput-:,20,output);i=0;do{Read(&buffer[i],1,input);}while(buffer[i++]!='\n');buffer[--i]='\0';if(Strncmp(buffer,exit,4)==0){Halt();}if(i0){newProc=Exec(buffer);Join(newProc);}}}Start.s:/*Start.s*AssemblylanguageassistforuserprogramsrunningontopofNachos.**Sincewedon'twanttopullintheentireClibrary,wedefine*whatweneedforauserprogramhere,namelyStartandthesystem*calls.*/#defineIN_ASM#includesyscall.h.text.align2/*-------------------------------------------------------------*__start*InitializerunningaCprogram,bycallingmain.**NOTE:Thishastobefirst,sothatitgetsloadedatlocation0.*TheNachoskernelalwaysstartsaprogrambyjumpingtolocation0.*-------------------------------------------------------------*/.globl__start.ent__start__start:jalmainmove$4,$0jalExit/*ifwereturnfrommain,exit(0)*/.end__start/*-------------------------------------------------------------*Systemcallstubs:*AssemblylanguageassisttomakesystemcallstotheNachoskernel.*Thereisonestubpersystemcall,thatplacesthecodeforthe*systemcallintoregisterr2,andleavestheargumentstothe*systemcallalone(inotherwords,arg1isinr4,arg2is*inr5,arg3isinr6,arg4isinr7)**Thereturnvalueisinr2.ThisfollowsthestandardCcalling*conventionontheMIPS.*-------------------------------------------------------------*/.globlHalt.entHaltHalt:addiu$2,$0,SC_Haltsyscallj$31.endHalt.globlAdd.entAddAdd:addiu$2,$0,SC_Addsyscallj$31.endAdd.globlExit.entExitExit:addiu$2,$0,SC_Exitsyscallj$31.endExit.globlExec.entExecExec:addiu$2,$0,SC_Execsyscallj$31.endExec.globlExecV.entExecVExecV:addiu$2,$0,SC_ExecVsyscallj$31.endExecV.globlJoin.entJoinJoin:addiu$2,$0,SC_Joinsyscallj$31.endJoin.globlCreate.entCreateCreate:addiu$2,$0,SC_Createsyscallj$31.endCreate.globlRemove.entRemoveRemove:addiu$2,$0,SC_Removesyscallj$31.endRemove.globlOpen.entOpenOpen:addiu$2,$0,SC_Opensyscallj$31.endOpen.globlRead.entReadRead:addiu$2,$0,SC_Readsyscallj$31.endRead.globlWrite.entWriteWrite:addiu$2,$0,SC_Writesyscallj$31.endWrite.globlClose.entCloseClose:addiu$2,$0,SC_Closesyscallj$31.endClose.globlSeek.entSeekSeek:addiu$2,$0,SC_Seeksyscallj$31.endSeek.globlDelete.entDeleteDelete:addiu$2,$0,SC_Deletesyscallj$31.endDelete.globlThreadFork.entThreadForkThreadFork:addiu$2,$0,SC_ThreadForksyscallj$31.endThreadFork.globlThreadYield.entThreadYieldThreadYield:addiu$2,$0,SC_ThreadYieldsyscallj$31.endThreadYield.globlThreadExit.entThreadExitThreadExit:addiu$2,$0,SC_ThreadExitsyscallj$31.endThreadExit.globlThreadJoin.entThreadJoinThreadJoin:addiu$2,$0,SC_ThreadJoinsyscallj$31.endThreadJoin.globlgetSpaceID.entgetSpaceIDgetSpaceID:addiu$2,$0,SC_getSpaceIDsyscallj$31.endgetSpaceID.globlgetThreadID.entgetThreadIDgetThreadID:addiu$2,$0,SC_getThreadIDsyscallj$31.endgetThreadID.globlIpc.entIpcIpc:addiu$2,$0,SC_Ipcsyscallj$31.endIpc.globlClock.entClockClock:addiu$2,$0,SC_Clocksyscallj$31.endClock/*dummyfunctiontokeepgcchappy*/.globl__main.ent__main__main:j$31.end__main.globlStrncmp.entStrncmpStrncmp:addiu$2,$0,SC_Strncmpsyscallj$31.endStrncmp.globlExit.entExitUserprog文件夹下Exception.cc://exception.cc//EntrypointintotheNachoskernelfromuserprograms.//Therearetwokindsofthingsthatcancausecontrolto//transferbacktoherefromusercode:////syscall--Theusercodeexplicitlyrequeststocallaprocedure//intheNachoskernel.Rightnow,theonlyfunctionwesupportis//Halt.////exceptions--TheusercodedoessomethingthattheCPUcan'thandle.//Forinstance,accessingmemorythatdoesn'texist,arithmeticerrors,//etc.////Interrupts(whichcanalsocausecontroltotransferfromuser//codeintotheNachoskernel)arehandledelsewhere.////Fornow,thisonlyhandlestheHalt()systemcall.//Everythingelsecoredumps.////Copyright(c)1992-1996TheRegentsoftheUniversityofCalifornia.//Allrightsreserved.Seecopyright.hforcopyrightnoticeandlimitation//ofliabilityanddisclaimerofwarrantyprovisions.#includecopyright.h#includemain.h#includesyscall.h#includeksyscall.h//----------------------------------------------------------------------//ExceptionHandler//EntrypointintotheNachoskernel.Calledwhenauserprogram//isexecuting,andeitherdoesasyscall,orgeneratesanaddressing//orarithmeticexception.////Forsystemcalls,thefollowingisthecallingconvention:////systemcallcode--r2//arg1--r4//arg2--r5//arg3--r6//arg4--r7////Theresultofthesystemcall,ifany,mustbeputbackintor2.////Ifyouarehandlingasystemcall,don'tforgettoincrementthepc//beforereturning.(Orelseyou'llloopmakingthesamesystemcallforever!)////whichisthekindofexception.Thelistofpossibleexceptions//isinmachine.h.//----------------------------------------------------------------------voidExceptionHandler(ExceptionTypewhich){inttype=kernel-machine-ReadRegister(2);DEBUG(dbgSys,ReceivedExceptionwhichtype:type\n);switch(which){caseSyscallException:switch(type){caseSC_Halt:DEBUG(dbgSys,Shutdown,initiatedbyuserprogram.\n);SysHalt();ASSERTNOTREACHED();break;caseSC_Write:DEBUG(dbgSys,Wri
本文标题:nachos实验一代码
链接地址:https://www.777doc.com/doc-5260911 .html