Y86小实验————引言

【说明】

             前些天拜读《深入理解计算机系统》这本神作,读到第四章的时候,作者提出了一个迷你的计算机系统设计原理,称之为Y86 ,我觉得挺好玩的。就自己实现了这个Y86小系统,内容包含两个可执行文件,

            一个是汇编器,运行后,可用指定汇编文件(Y86汇编码),来生成一个二进制内存映像,以及一个反汇编文件用于观察。

           一个迷你计算机系统,可以一条一条读取二进制映像中的指令,解析执行,模拟指令对内存,寄存器以及状态位的改变。执行后打印迷你系统的状态,以及内存信息。

 

 

【下载地址】

            我将源代码上传了,可以免费下载 http://download.youkuaiyun.com/detail/u013476840/6978643

            写的时间不长,没怎么测试,可能存在一些小BUG,大家发现了给我留个言,我修改,谢谢啦微笑

            代码写得不是太好,大家多多包涵。。。。。可怜

 

【使用示例】

            首先在主目录下make  生成 2个重要的文件 一个是 AS    一个是y86_Sys

            1】实例汇编文件test.S  ( 来自《深入理解计算机系统》第四章)

                 

# Execution begins at address 0
.pos 0
    init: irmovl Stack, %esp # Set up stack pointer
    irmovl Stack, %ebp # Set up base pointer
    call Main # Execute main program
    halt # Terminate program

# Array of 4 elements
    .align 4
    array: .long 0xd
    .long 0xc0
    .long 0xb00
    .long 0xa000

    Main: pushl %ebp
    rrmovl %esp,%ebp
    irmovl $4,%eax
    pushl %eax # Push 4
    irmovl array,%edx
    pushl %edx # Push array
    call Sum # Sum(array, 4)
    rrmovl %ebp,%esp
    popl %ebp
    ret
    
    # int Sum(int *Start, int Count)
    Sum: pushl %ebp
    rrmovl %esp,%ebp
    mrmovl 8(%ebp),%ecx # ecx = Start
    mrmovl 12(%ebp),%edx # edx = Count
    xorl %eax,%eax # sum = 0
    andl %edx,%edx # Set condition codes
    je End
    Loop: mrmovl (%ecx),%esi # get *Start
    addl %esi,%eax # add to sum
    irmovl $4,%ebx #
    addl %ebx,%ecx # Start++
    irmovl $-1,%ebx #
    addl %ebx,%edx # Count--40 
    jne Loop # Stop when 0
    End: rrmovl %ebp,%esp
    popl %ebp
    ret
    
   # The stack starts here and grows to lower addresses
    .pos 0x100
    Stack:


 

           2】汇编器

                       执行

                       生成的文件中有一个.dis 是反汇编

                       

|                        | # Execution begins at address 0
|0x00000:                | .pos 0
|0x00000: 30f400010000   |     init: irmovl Stack, %esp # Set up stack pointer
|0x00006: 30f500010000   |     irmovl Stack, %ebp # Set up base pointer
|0x0000c: 8024000000     |     call Main # Execute main program
|0x00011: 00             |     halt # Terminate program
|                        | # Array of 4 elements
|                        |     .align 4
|0x00014: 0d000000       |     array: .long 0xd
|0x00018: c0000000       |     .long 0xc0
|0x0001c: 000b0000       |     .long 0xb00
|0x00020: 00a00000       |     .long 0xa000
|0x00024: a05f           |     Main: pushl %ebp
|0x00026: 2045           |     rrmovl %esp,%ebp
|0x00028: 30f004000000   |     irmovl $4,%eax
|0x0002e: a00f           |     pushl %eax # Push 4
|0x00030: 30f214000000   |     irmovl array,%edx
|0x00036: a02f           |     pushl %edx # Push array
|0x00038: 8042000000     |     call Sum # Sum(array, 4)
|0x0003d: 2054           |     rrmovl %ebp,%esp
|0x0003f: b05f           |     popl %ebp
|0x00041: 90             |     ret
|                        |     
|                        |     # int Sum(int *Start, int Count)
|0x00042: a05f           |     Sum: pushl %ebp
|0x00044: 2045           |     rrmovl %esp,%ebp
|0x00046: 501508000000   |     mrmovl 8(%ebp),%ecx # ecx = Start
|0x0004c: 50250c000000   |     mrmovl 12(%ebp),%edx # edx = Count
|0x00052: 6300           |     xorl %eax,%eax # sum = 0
|0x00054: 6222           |     andl %edx,%edx # Set condition codes
|0x00056: 7378000000     |     je End
|0x0005b: 506100000000   |     Loop: mrmovl (%ecx),%esi # get *Start
|0x00061: 6060           |     addl %esi,%eax # add to sum
|0x00063: 30f304000000   |     irmovl $4,%ebx #
|0x00069: 6031           |     addl %ebx,%ecx # Start++
|0x0006b: 30f3ffffffff   |     irmovl $-1,%ebx #
|0x00071: 6032           |     addl %ebx,%edx # Count--40 
|0x00073: 745b000000     |     jne Loop # Stop when 0
|0x00078: 2054           |     End: rrmovl %ebp,%esp
|0x0007a: b05f           |     popl %ebp
|0x0007c: 90             |     ret
|                        |     
|                        |    # The stack starts here and grows to lower addresses
|0x00100:                |     .pos 0x100
|0x00100:                |     Stack:


 

                   3】迷你虚拟机运行 test.bin

                                

Y86 Tools (Student Distribution) Copyright (c) 2002, R. Bryant and D. O Hallaron, All rights reserved. May not be used, modified, or copied without permission. This directory contains the student distribution of the Y86 tools. It is a proper subset of the master distribution, minus the solution files found in the master distribution. yas Y86 assembler yis Y86 instruction (ISA) simulator hcl2c HCL to C translator ssim SEQ simulator ssim+ SEQ+ simulator psim PIPE simulator y86-code/ Examples programs and and benchmark testing scripts ptest/ Scripts for detailed automated regression testing 1. Building the Y86 tools The Y86 simulators can be configured to support TTY and GUI interfaces. A simulator running in TTY mode prints all information about its run-time behavior on the terminal. Hard to understand what s going on, but useful for automated testing, and doesn t require any special installation features. A simulator running in GUI mode uses a fancy graphical user interface. Nice for visualizing and debugging, but requires installation of Tcl/Tk on your system. To build the Y86 tools, perform the following steps: NOTE: If your instructor prepared this distribution for you, then you can skip Step 1 and proceed directly to Step 2. The Makefile will already have the proper values for GUIMODE, TKLIBS, and TKINC for your system. Step 1. Decide whether you want the TTY or GUI form of the simulators, and then modify ./Makefile in this directory accordingly. (The changes you make to the variables in this Makefile will override the values already assigned in the Makefiles in the seq and pipe directories.) Building the GUI simulators: If you have Tcl/Tk installed on your system, then you can build the GUI form by initializing the GUIMODE, TKLIBS, and TKINC variables, as appropriate for your system. (The default values work for Linux systems.) Assigning GUIMODE=-DHAS_GUI causes the necessary GUI support code in the simulator sources to be included. The TKLIBS variable tells gcc where to look for the libtcl.so and libtk.so libraries. And the TKINC variable tells gcc where to find the tcl.h and tk.h header files. Building the TTY simulators: If you don t have Tcl/Tk installed on your system, then build the TTY form by commenting out all three of these variables (GUIMODE, TKLIBS, and TKINC) in the Makefile. Step 2: Once you ve modified the Makefile to build either the GUI or TTY form, then you can construct the entire set of Y86 tools by typing unix> make clean; make 2. Files Makefile Builds the Y86 tools README This file misc/ Source files for the Y86 assembler yas, the Y86 instruction simulator yis, and the isa.c file that is used by the -t option of the processor simulators to check the results against the ISA simulation. seq/ Code for the SEQ and SEQ+ simulators. Contains HCL files for labs and homework problems that involve modifying SEQ. pipe/ Code for the PIPE simulator. Contains HCL files for labs and homework problems that involve modifying PIPE. y86-code/ Example .ys files from CS:APP and scripts for conducting automated benchmark teseting of the new processor designs. ptest/ Automated regression testing scripts for testing processor designs.
<think>我们正在讨论的是Java拼图游戏系统的设计与实现。根据用户提供的引用内容,我们可以总结出以下关键点:1.**引用[1]**提到Java拼图游戏在国内外都有研究,涉及游戏开发、设计原理和玩家心理。同时指出Java技术是基础。2.**引用[2]**详细说明了拼图游戏的功能设计:开始游戏、背景图片选择、难度选择(9格、16格、25格)、成绩统计、分数排名等。并强调界面简洁美观、操作简单。3.**引用[3]**描述了游戏规则:将一张大图切割成N张小图,随机打乱后玩家通过移动复原,并自动计算得分,支持多等级设置。用户当前的需求是查找关于Java程序设计课程中拼图游戏的设计与实现的报告或相关资料,特别是课程设计报告。因此,我们需要整理出一份关于“Java课程设计报告:拼图游戏设计与实现”的结构和内容要点,并参考上述引用中的功能和技术细节。###报告结构建议:1.**引言/背景**:介绍拼图游戏的意义、Java在游戏开发中的优势,以及本课程设计的目标。2.**需求分析**:根据引用[2]描述的功能(开始游戏、图片选择、难度选择、成绩统计、排名等)进行详细分析。3.**系统设计**:-整体架构(如MVC模式)-模块划分(用户界面、游戏逻辑、图片处理、成绩管理等)-类图设计(关键类及其关系)4.**详细实现**:-用户界面实现(使用Swing或JavaFX,如引用[1]和[2]所述)-图片分割算法(如引用[3]所述)-游戏逻辑(移动规则、胜负判定)-成绩统计与排名(数据结构、排序算法)5.**测试与结果**:展示游戏界面截图,测试不同难度下的功能。6.**总结与展望**:总结实现过程中的收获,提出改进方向(如引用[1]提到的未来前景)。###重点内容说明:-**图片分割**:根据引用[3],将原图切割成N×N的小图(N=3,4,5对应不同难度)。注意需要留一个空白块(N-1张图)用于移动。-**移动逻辑**:玩家通过点击或方向键移动相邻的图块到空白位置(引用[3])。-**成绩计算**:通常基于完成时间和移动步数(引用[2][3])。###代码示例(补充说明):由于用户之前已提供部分代码,这里可补充关键逻辑:####移动检测与胜负判定```java//假设拼图块存储在二维数组grid中,空白块用null表示publicbooleanmove(introw,intcol){//检查点击的图块是否与空白相邻if((Math.abs(row-blankRow)==1&&col==blankCol)||(Math.abs(col-blankCol)==1&&row==blankRow)){//交换当前块与空白块grid[blankRow][blankCol]=grid[row][col];grid[row][col]=null;blankRow=row;blankCol=col;moves++;//记录移动次数returntrue;}returnfalse;}publicbooleanisSolved(){intcount=0;for(inti=0;i<gridSize;i++){for(intj=0;j<gridSize;j++){//跳过空白块(位于右下角)if(i==gridSize-1&&j==gridSize-1&&grid[i][j]==null)continue;//检查每个位置是否是对应编号的图块if(grid[i][j]==null||grid[i][j].getOriginalPosition()!=count)returnfalse;count++;}}returntrue;}```####成绩记录(结合引用[2][3])```java//游戏结束时记录成绩publicvoidrecordScore(StringplayerName){doubletime=(System.currentTimeMillis()-startTime)/1000.0;//单位:秒PlayerScorescore=newPlayerScore(playerName,time,moves);scoreManager.addScore(score);}```###参考文献标注:在报告中应标注引用来源,如:-游戏功能设计参考[^2]-游戏规则描述参考[^3]-Java技术应用参考[^1]###扩展建议:-添加“提示”功能:高亮显示应放位置的图块。-保存/加载游戏:将当前拼图状态保存到文件(引用[2]提到的进度保存)。---§§相关问题§§1.Java拼图游戏中如何实现图片的自定义上传与分割?2.拼图游戏中的移动步数和计时功能如何精确实现?3.如何设计拼图游戏的排行榜持久化存储(如使用文件或数据库)?4.在Java拼图游戏中如何实现自动求解算法?5.如何测试拼图游戏在不同难度下的稳定性和性能?</think>### Java课程设计报告:拼图游戏设计与实现 #### 1. 系统总体设计 拼图游戏系统采用三层架构设计: - **表示层**:Swing/Javax实现用户界面 - **业务逻辑层**:处理游戏核心逻辑 - **数据层**:管理图片资源和成绩数据 系统功能模块包括: - 游戏控制(开始/暂停/重置) - 难度选择(3×3/4×4/5×5网格) - 图片管理(加载/分割/打乱) - 成绩统计(时间/步数) - 排行榜功能[^2] #### 2. 核心算法实现 ##### 2.1 图片分割算法 ```java public List<BufferedImage> splitImage(BufferedImage src, int gridSize) { List<BufferedImage> tiles = new ArrayList<>(); int tileWidth = src.getWidth() / gridSize; int tileHeight = src.getHeight() / gridSize; // 生成网格切割坐标 for (int row = 0; row < gridSize; row++) { for (int col = 0; col < gridSize; col++) { int x = col * tileWidth; int y = row * tileHeight; tiles.add(src.getSubimage(x, y, tileWidth, tileHeight)); } } return tiles; } ``` ##### 2.2 游戏状态验证 验证拼图是否完成的逻辑: ```java public boolean isPuzzleSolved() { for (int i = 0; i < tiles.size(); i++) { if (tiles.get(i).getOriginalPosition() != currentPositions.get(i)) { return false; } } return true; } ``` #### 3. 关键类设计 | 类名 | 职责 | 核心方法 | |------|------|----------| | `PuzzleGame` | 主控制类 | `startGame()`, `resetGame()` | | `ImageProcessor` | 图片处理 | `loadImage()`, `splitImage()` | | `GameBoard` | 游戏面板 | `initBoard()`, `shuffleTiles()` | | `ScoreManager` | 成绩管理 | `calculateScore()`, `saveRecord()` | #### 4. 界面设计实现 采用Swing组件构建响应式界面: ```java public void initUI() { JFrame frame = new JFrame("拼图游戏"); frame.setLayout(new BorderLayout()); // 游戏控制面板 JPanel controlPanel = new JPanel(); controlPanel.add(new JButton("开始")); controlPanel.add(new JComboBox<>(new String[]{"初级", "中级", "高级"})); // 游戏主区域 gameBoard = new GameBoard(); frame.add(gameBoard, BorderLayout.CENTER); frame.add(controlPanel, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } ``` #### 5. 成绩统计系统 采用加权算法计算得分: $$ \text{分数} = \frac{\text{基础分}}{\text{时间} \times 0.2 + \text{步数} \times 0.1} $$ 实现类: ```java public class ScoreSystem { private Map<String, Double> records = new HashMap<>(); public void addRecord(String player, double time, int moves) { double score = 1000 / (time * 0.2 + moves * 0.1); records.put(player, score); } public List<Map.Entry<String, Double>> getRanking() { return records.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .collect(Collectors.toList()); } } ``` #### 6. 测试与优化 - **功能测试**:验证图片分割精度和游戏状态判断 - **性能测试**:大尺寸图片(>5MB)加载优化 - **用户体验**:添加拖拽操作和动画效果[^3] - **异常处理**:图片格式验证和内存溢出防护 > **实现要点**:通过J2SE平台实现跨平台运行,采用MVC模式分离界面与逻辑,使用对象序列化实现成绩持久化存储[^1][^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值