• Advertisement
Sign in to follow this  

Questions About Storing Opcodes With Operands

This topic is 647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I'm puppysss, I really wonder how cpu store and load any opcodes with operands to RAM and Registers.

I know that PC ( Program Counter ) in cpu points next opcodes and program decoder run a current opcode.

but don't know how any opcodes are stored and what is the rule of storing opcodes in RAM.

( I saw Procedures of seperating opcodes sector was in Assembly Source Code. )

Share this post


Link to post
Share on other sites
Advertisement

Opcodes are stored in the virtual address space of the process. They are simply data, with unique number for each opcode and variation. Parameters - or the actual data to be processed - can reside in other parts of the process address space.

 

If you open a debugger on a running process and go to memory view, you can see the actual opcodes in the binary section of the process address space. Or, you can dump an executable without running it and view the program as a chunk of memory.

 

The CPU has instructions to copy data between RAM and registers, and these instructions themselves have their own opcodes.

Edited by Nik02

Share this post


Link to post
Share on other sites

At least CPU itself dosent load program code rather than the OS does in special memory location where the instruction pointer stays. It even dosent know about parameters if they were dynamic. If you write

x = a + b

would lead the OS to push a's address onto the call stack and b's address before operating the addition and push the result back into x's memory location. Any other instructions like the CPU specific operation byte for add-operator are cpu specific and based on the physical instruction pipeline branded onto the cpu core

Share this post


Link to post
Share on other sites

I really wonder how cpu store and load any opcodes with operands to RAM and Registers. I know that PC ( Program Counter ) in cpu points next opcodes and program decoder run a current opcode. but don't know how any opcodes are stored and what is the rule of storing opcodes in RAM.

The CPU manufacturer (eg Intel or AMD) defines that. See eg the 80386 https://en.wikipedia.org/wiki/Intel_80386
First external reference there is the "Intel 80386 Programmer's Reference Manual 1986 (PDF)

 

That document described in full detail how to program the 80386 at stored program level. Chapter 17 explains of each opcode what it exactly does, and how it is stored in memory.

So if you want to run a program on a 386, this is what you have to provide.

 

Typically, each instruction is encoded in a number of bytes in memory (the pdf defines exactly how for the 80386 processor). The CPU that you buy assumes that where-ever you point its instruction counter at, the data it finds there is program code (encoded as specified). If it can successfully decode an opcode, that opcode can be executed. If it fails, you typically get some sort of "illegal-instruction" exception, see Section 9.8.6 of the PDF :)

Share this post


Link to post
Share on other sites

the virtual address space of the process

thanks I understood, so is the virtual address space in RAM ? and seperated from any data ?

 

to push a's address onto the call stack

umm I really don't know what purpose the call stack is. is that true the call stack memory is in RAM and CPU provides stack operations?

 

If it can successfully decode an opcode, that opcode can be executed. If it fails, you typically get some sort of "illegal-instruction" exception

wow I've never known that :)

Share this post


Link to post
Share on other sites

Opcodes formats depend on the specification.

 

Machine code opcodes are simply inputs to logic gates. Normally, the first few bits determines the operation, followed by a few register fields. Should the opcode require data from memory then there will be a bit of math depending on the instruction. The opcode will normally store the memory address in the last bits.

 

Take MIPS for example. The memory address is stored in the 16bit immediate address [15-0] of all I types in little endian format. I types are usually used for access into the main memory.

Edited by Tangletail

Share this post


Link to post
Share on other sites

umm I really don't know what purpose the call stack is. is that true the call stack memory is in RAM and CPU provides stack operations?

 

 

Yes and yes. The call stack is pushed when you enter a function and it contains that function's local variables. The 'stack frame' for that function also includes some data about where the function was called from so that when the function returns the stack is popped and it has the information about where to go to in the bytecode in order to return to the calling function.

 

If you compile some simple, minimal functions in Visual Studio you can put a breakpoint in the program and run it, then when the debugger stops at the breakpoint you can right-click on the code and 'View disassembly' to get a look at the assembly that pushes and pops the stack (the instructions in x86 are 'call' and 'ret'). There's an option to also view the compiled bytecode for each instruction.

 

You can also add registers to the variable watch list, and view the call stack in the memory viewer. If you take things one instruction at a time you should be able to work out what it's doing. Just note that the stack 'grows' backward in memory. That is, it starts at a higher address and when it's pushed to the new data goes into lower addresses.

 

[attachment=32585:86c0b220ca.png]

Share this post


Link to post
Share on other sites

The opcode will normally store the memory address in the last bits.
thanks I understand now perfectly!:)

 

@ Tangletail : thanks:)

@ Khatharr : thank you so much!:) I'm perfectly understood!

Share this post


Link to post
Share on other sites

 

to push a's address onto the call stack

umm I really don't know what purpose the call stack is. is that true the call stack memory is in RAM and CPU provides stack operations?

 

 

 

depending on the bootloader and the OS there is anywhere a general call stack in memory. I moodled a bit 7 years ago on thos topic writing an own bootloader and operation kernel. Setting up the callstack in the boot process looked like this one

mov ax,cs     ;Setup segment registers
mov ds,ax     ;Make DS correct
mov es,ax     ;Make ES correct
mov ss,ax     ;Make SS correct 
mov bp,7c00h
mov sp,7c00h  ;Setup a stack

Share this post


Link to post
Share on other sites

there is anywhere a general call stack in memory

thanks now I get that call stack memory is in some cpu and cpu must have a counter for stack algorithm. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement