8. Using Jasmin to Run x86 Assembly Code 8.使用Jasmin运行x86汇编代码

What You Need for This Project这个项目需要什么

Purpose目的

To practice writing and running basic x86 assembly code, using the Jasmin interpreter.使用Jasmin解释器练习编写和运行基本x86汇编代码。

Understanding the Jasmin Window了解Jasmin窗口

You can download Jasmin here .你可以在这里下载Jasmin。

Double-click the Jasmin-1.5.8.jar file you downloaded.双击下载的Jasmin-1.5.8.jar文件。

Jasmin launches, with a cringe-worthy pinup on it.茉莉发布了一个令人生畏的海报。

Click the " New File " button.点击“ 新建文件 ”按钮。

Look over the window, referring to the diagram below:查看窗口,参考下图:

Find and examine these sections:查找并检查这些部分:

Registers寄存器

Data used during processing is stored in the registers EAX , EBX , ECX , and EDX .处理期间使用的数据存储在寄存器EAXEBXECXEDX中

The ESP (Extended Stack Pointer) contains the address of the top of the Stack. ESP (扩展堆栈指针)包含堆栈顶部的地址。

The EIP (Extended Instruction Pointer) contains the address of the the next instruction to be processed. EIP (扩展指令指针)包含要处理的下一条指令的地址。

Flags

These one-bit values that are used for branching.这些用于分支的一位值。 For example the JZ instruction will jump if the Zero flag is 1 (set), and the JNZ instruction will jump if the Zero flag is 0 (cleared).例如,如果Zero标志为1(设置),则JZ指令将跳转,如果Zero标志为0(清零),则JNZ指令将跳转。

Code

This is where you type in commands, such as mov eax,4这是您键入命令的位置,例如mov eax,4

Help帮帮我

Help messages appear here.帮助信息出现在这里。

Memory记忆

This processor has 0x1000 = 4096 bytes of RAM, which is not enough to run complete modern programs, but plenty for running little assembly programs for learning purposes.该处理器具有0x1000 = 4096字节的RAM,这不足以运行完整的现代程序,但足以运行用于学习目的的小程序集。

With the Memory pane scrolled to the top, as shown in the image above, you see memory that the program will use to store data during processing.当Memory窗格滚动到顶部时,如上图所示,您会看到程序在处理过程中将用于存储数据的内存。

Scroll this pane to the bottom to see the Stack, which starts at address 0x1000 and grows downward.将此窗格滚动到底部可查看从地址0x1000开始并向下增长的堆栈。

Using mov Instructions使用mov指令

In the Code section, type in these instructions.在代码部分,输入这些说明。
mov eax, 4 mov eax,4
mov ebx, 6 mov ebx,6
These instructions move the number 4 into eax, and the number 6 into ebx.这些指令将数字4移入eax,数字6移入ebx。

At the top of the Jasmin window, click the green Run button, as shown below.在Jasmin窗口的顶部,点击绿色的运行按钮,如下所示。

The program runs.程序运行。 When it stops, notice these things, as shown below:当它停止时,注意这些事情,如下所示:

Troubleshooting故障排除

If you make an error in an instruction, the program will stop prematurely.如果在指令中发生错误,程序将提前停止。 Fix the instruction, and click the Reset button.修复指令,然后单击重置按钮。 Then you can run it again.然后你可以再次运行它。

Storing Results in Memory将结果存储在内存中

Add more lines to your Code section to make your program look like this:将更多行添加到代码部分,以使您的程序如下所示:
mov eax, 4 mov eax,4
mov ebx, 6 mov ebx,6
mov [eax], ebx mov [eax],ebx
mov ecx, eax mov ecx,eax
add ecx, ebx添加ecx,ebx
mov [eax+4], ecx mov [eax + 4],ecx
Here's what these instructions do:以下是这些说明的作用:
mov eax, 4 Move the value 4 into eax将值4移至eax
mov ebx, 6 Move the value 6 into ebx将值6移至ebx
mov [eax], ebx Move the value in ebx (which is 6) into the memory location pointed to by eax (memory location 4)将ebx中的值(即6)移入由eax指向的内存位置(内存位置4)
mov ecx, eax Move the value in eax (which is 4) into ecx将eax中的值(它是4)移到ecx中
add ecx, ebx Add the value in ebx (which is 6) to the value in ecx (which is 4), and put the result into ecx (the result is 10)将ebx中的值(它是6)添加到ecx(它是4)中的值,并将结果放入ecx(结果为10)
mov [eax+4], ecx Move the value in ecx (which is 10) into the memory location four past the location pointed to by eax (memory location 8)将ecx中的值(10)移到eax指向的位置(内存位置8)之后的四个存储单元中,
Run the program.运行该程序。 When it completes, you should see these results, as shown below:完成后,您应该看到这些结果,如下所示:

Using the Stack使用堆栈

In Jasmin, click File , New .在Jasmin中,单击文件新建

In the Code section, type in these instructions.在代码部分,输入这些说明。

mov eax, 4 mov eax,4
mov ebx, 6 mov ebx,6
push eax推eax
push ebx推ebx
Before running the program, notice the ESP: it contains 4096, as shown below.在运行程序之前,请注意ESP:它包含4096,如下所示。

4096 is 0x1000 in hexadecimal--this is where the Stack ends. 4096是十六进制的0x1000 - 这是堆栈结束的地方。

Scroll down in the Memory pane to see the last values.在“内存”窗格中向下滚动以查看最后的值。 As show above, the last location is at 0xFFC.如上所示,最后一个位置是0xFFC。 This value is 32 bits long, so it contains four bytes, at locations 0xFFC, 0xFFD, 0xFFE, and 0xFFF.该值为32位长,因此它包含4个字节,位置为0xFFC,0xFFD,0xFFE和0xFFF。 The ESP points to the next byte, 0x1000. ESP指向下一个字节0x1000。

Understanding Push理解推送

At the top of the Jasmin window, click the green Run button.在Jasmin窗口的顶部,点击绿色的运行按钮。

These instructions move the number 4 into eax, and the number 6 into ebx.这些指令将数字4移入eax,数字6移入ebx。 Then both values are pushed onto the stack.然后将这两个值推入堆栈。

Notice these things, as shown below:注意这些事情,如下所示:

Understanding Pop理解Pop

Add a pop instruction to your code, so it now looks like this:为你的代码添加一个pop指令,现在看起来像这样:
mov eax, 4 mov eax,4
mov ebx, 6 mov ebx,6
push eax推eax
push ebx推ebx
pop ecx pop ecx
Run the code.运行代码。

Notice these things, as shown below:注意这些事情,如下所示:

Reversing a Sequence反转序列

In Jasmin, click File , New .在Jasmin中,单击文件新建

In the Code section, type in these instructions.在代码部分,输入这些说明。

mov eax, 1 mov eax,1
mov ebx, 2 mov ebx,2
mov ecx, 3 mov ecx,3
mov edx, 4 mov edx,4
push eax推eax
push ebx推ebx
push ecx推ecx
push edx推edx
pop eax流行eax
pop ebx流行ebx
pop ecx pop ecx
pop edx流行edx
These instructions load values into the four registers, push them onto the stack in order, and pop them off the stack in order.这些指令将值加载到四个寄存器中,按顺序将它们推入堆栈,然后按顺序将它们从堆栈中弹出。

However, since the stack is a FILO (First In, Last Out) structure, this reverses the order of the values.但是,由于堆栈是一个FILO(先入先出)结构,这会颠倒这些值的顺序。

Push the Step four times to execute only the first four instructions, as shown below:按下这个步骤四次只执行前四条指令,如下所示:

You see the values 1, 2, 3, and 4 loaded into the EAX, EBX, ECX, and EDX registers, as shown below.您可以看到EAX,EBX,ECX和EDX寄存器中加载的值1,2,3和4,如下所示。

Push the Step four more times to execute only the next four instructions.再推四 ,只执行下四条指令。

You see the values 1, 2, 3, and 4 pushed onto the stack, as shown below.您会看到将值1,2,3,和4推入堆栈,如下所示。

Push the Step four more times to execute the remaining four instructions.再推四执行其余四条指令。

Now the registers contain these values:现在寄存器包含这些值:

as shown below.如下所示。


Last modified 7-29-17最后修改7-29-17