I want to thank you for your video!!! I studied this long time ago, but it is wonderful you decided to post this video so more people can understand how a computer works!!! Information and understanding it, is important in human development!!!! THANK YOU
Truly the most brilliant video series I have ever seen when it comes to explaining this topic. I can't believe how much I appreciate them. Thank you so much!
AWESOME video, just subbed! I’m a CS guy that loves the CE domain more than anything. This video has really helped me, getting ready to start writing a 6502 emulator. I will for sure check out your other videos as well. Cheers!
I am not a CS major but rather an MIS major, it's cousin. I chose MIS to make getting an MBA easier in the future, but man, do I love coding from the bottom of my heart. It's awesome, and high level coding comes to me pretty naturally. Even if I will never use it, I want to learn low level coding too. It's fascinating. Now if only I could understand it just a bit better >.>
Beautiful! I want to one day program in binary. Literally. Even just a little. Thank you for showing binary representations of some the assembly instructions.
I get how you know that 01H is 00000001 in binary... But how do you convert the LD A into binary? Or is it something your remember from a list? I have to convert it into machine code, is machine code binary?
This is a very good question. Machine code is the language of the machine (I.e low and high voltage levels) which as humans we represent as binary. Every machine code instruction has it's own binary number. Load A with immediate data has the binary number 00111110 which is 3E in hexadecimal (an easy way to remember binary). So every instruction has it's own binary number. Every instruction has it's own binary number so in a sense it comes from a list as you suggest. However, in truth each number makes the electronics behave in a specific way. The binary patterns open and close electronic solid state switches that perform a simple task that when all executed in a specific order are able to implement sophisticated functionality.
John Philip Jones So does LOAD have it's own binary number? Or for example do LOAD A, LOAD B and LOAD C count as three different things with three separate binary numbers representing them? And does this mean that there is no way of mathematically converting LOAD A into binary in your head/on paper without a table that literally tells you which binary number represents each op code I think it's called?
malteeaser101 LD A, N has its own binary number called an Operation code. LD B, N has a different operation code. When writing assembly language program you type in instructions such as LD A, N and an assembler (computer program) converts the assembly language to machine code (i.e. binary patterns). You can also do the conversion as a human it is called hand assembly and it means you would have to either know all the codes or access them from a printed list.Back in the day I used to code in assembly and as sad as it may sound I knew all of the codes. But there was only around 70 different instructions for the machines I was working on.
Mo Fo Hello Mo, Every 'type' of CPU (processor) has its own machine code language and related assembly language. For example Intel processors are different to Motorola processors and have a different machine code language from one another. The description I have put forward is based on the Zilog Z80 processor which also has its own machine code language. I am unsure what machine code language you are referring to but what you are describing is what is often called absolute addressing The term addressing refers to how data is accessed by an individual machine code instruction. I have a short series of videos that show the difference. I have put links to their playlist below: ua-cam.com/play/PL6lxxT7IdTxGXX6D6j-zjzcgoOD8WYMS2.html Phil
Nicely done. But @5:54 the name “Immediate” actually refers to the operand being the actual data to operate on. As opposed to the operand being an address, or an offset, that points to the data (or further indirection). Specifically, “Immediate” does not mean the operand immediately follows the opcode, as you have stated. Clearly, the operand always immediately follows the opcode. The operand just isn’t always the “immediate” actual data to operate on.
What you are referring to is addressing modes. If the data to be processed by the instruction follows the opcode, then the addressing mode is referred to as immediate addressing. Another instruction can have the address of the data just after the opcode. This address gives the memory address of where the data to be processed can be located. This type of instruction is said to have an absolute addressing mode. Immediate addressing is covered by the video at the link below: ua-cam.com/video/C6P2s5J9RaM/v-deo.htmlsi=LuW6LxR811gcGlZ_ Absolute addressing is covered by the video at the link below: ua-cam.com/video/L8RVT0zG8Zk/v-deo.htmlsi=EsyWTnOtZBb8ThUi As an aside the addressing mode shown in the video at the link below is an example of implied addressing. ua-cam.com/video/XM4lGflQFvA/v-deo.htmlsi=CwcaGDV0h8ju9Kso Best wishes Phil
@@johnphilipjones Hi Phil. I do understand all of that. I’m old school. I started off hand-assembling machine code on my own self-built 8-bit microprocessor designs in the late 70’s. So I do have a pretty solid understanding of all of this. I was just trying to give some helpful feedback for your students. What I was pointing out is that @5:54 you specifically say: “the name immediate addressing comes from the fact that the operand comes immediately after the opcode”. This statement is potentially confusing to learners. i.e. The fact that the operand comes immediately after the opcode is a general statement, not specific to the addressing mode. I believe what you meant to say is more along the lines of: “the name immediate addressing comes from the fact that the operand is the actual data to be processed by the opcode”. This would then be consistent with what you have now clarified in your reply above. Hopefully you take this as the helpful constructive feedback I'd intended. 🤓
Hello Greg, Yes you are correct I missed your point initially. I am always happy for feedback and comments. It was, and is, taken as constructive. Best wishes Phil
Great stuff! Thanks. Is it necessary to convert instructions and operands to hex numbers? Can you keep instructions as binary code instead of 3E or something like that?
+zinturis You would normally code in assembly language and this is converted to the necessary binary codes by the assembler. So in the machine you would actually see binary and not hexadecimal. However, it is usual to look at the binary in hexadecimal as it is easier for us to remember hexadecimal numbers rather than long binary numbers and it is also easy to convert between binary and hexadecimal. The following videos look at converting between Hexadecimal and binary: ua-cam.com/video/fhgk2rxrn-Q/v-deo.html ua-cam.com/video/V-he1Ip36OA/v-deo.html Best wishes Phil (John Philip Jones)
Thanks a lot for the quick answer. Sorry if my questions may sound stupid, I am quite newbie in this but if you want to send certain instructions of machine code to the processor, what's the workflow of that? Can you do it from some terminal or do you need to create a text file and save as .bin or .. how that works in practice? I mean, how to control that process?
+zinturis You use an editor and type in your assembly language program and save it in a text file and use an assembler to convert your typed in program to machine code. You will need to download an assembler to match the CPU of your computer. However, a good way to learn is to download a CPU emulator that has an assembler incorporated. I used to do assembly programming on a Z80 flight micro-board, a 6511 micro-controller using an in-circuit emulator. However, these have now been superseded and I am not sure what is used today. However, I believe that the Raspberry Pi has a set up that allows for machine coding. I would recommend searching with the phrase "machine code emulator" and see what is out there to assist. Hope this helps. Best wishes Phil (John Philip Jones)
From your earlier respond, I understand that assembly language is different for different CPUs? So, if I write z=x+y for Intel it is not gonna work on an AMD based computer? So what's different? Assembly commands or register addresses?
zinturis z = x + y is a high level statement and will be converted to the machine code of the CPU that is 'in' your computer by a compiler. However, putting this aside the opcode for add on each different type of CPU is different. Each processor also has a different register set i.e. different number of registers and of different lengths. The architecture of the CPU's can also be different some are RISC other CISC or a combination of both CISC and RISC.
Very nice. So we can either write out the assembler on a piece of paper, then convert on the paper and then maybe (in the early days) use switches to set each memory address with the correct sequence of binary bits ? The part I still get stuck on is that the assembler can also be a program to convert the codes for us. Is this a program that would be loaded into memory, and therefore the first assemlber would have had to have been hand written (from what I've read, but still a little confused )and manually placed in the memory ?
When you switch on a computer the electronics generates an electrical pulse. This pulse is applied to a CPU pin that causes a machine code instruction to be fetched from a specific memory location. This instruction is the first instruction of the boot program other instruction are then fetched decoded and executed in order one after another. This boot program then sets up the operating system by loading other programs that form the operating system. Once the operating system is in place it is able to load up other programs (such as the assembler) at the request of the user (i.e by clicking on an icon or by issuing a command). In the very early days switches were indeed throw to enter the program you wished to execute.
The registers actually hold binary patterns. It is usual to show these patterns in their hexadecimal form because as humans it is difficult for us to remember long binary patterns. It is much easier to remember hexadecimal and it is relatively easy to convert between binary and hexadecimal. Best wishes Phil
Thank you sir for the video. But my question still is, how do you convert the assembly language code to machine code (opcode).Your reply will be much appreciated.
+Hammad Ahmed You write your program in assembly language and an assembler converts the assembly language to machine code. Regards Phil (John Philip Jones)
Danish Bhatia All microprocessors have an instruction set. This is a list of instructions in together with there hexadecimal equivalent. The link below will take you to an instruction set in PDF format which can be downloaded.
Danish Bhatia oops this is the link: www.zilog.com/appnotes_download.php?FromPage=DirectLink&dn=UM0080&ft=User%20Manual&f=YUhSMGNEb3ZMM2QzZHk1NmFXeHZaeTVqYjIwdlpHOWpjeTk2T0RBdmRXMHdNRGd3TG5Ca1pnPT0=
I want to know about how machine code is executed. I mean, since you already have these 1s and 0s in RAM, how can the CPU tell from so many instructions and do different actions correspondingly?
+Simon Lee When you switch on a microprocessor system (e.g. a computer) the hardware will always look in the same memory location. This location must hold the first operation code to be executed i.e. the program to be executed starts at this memory location. For a computer this is the boot program. For an embedded system containing a microprocessor this location would contain the first operation code for the first program to be executed.
+John Philip Jones I am sorry, but I am still confused. I mean how the CPU can know these 1s or 0s mean MOV, ADD or SUB etc. How do 1s and 0s make sense to the CPU and indicate the CPU what to do?
+Simon Lee A microprocessor system is a collection of million of switches that control the movement of binary patterns along bus systems, In addition these switches can be arranged to perform logic and arithmetic operations. The control of these switches is achieved by another lower level language that is not machine code. However, the machine code is able to dictate which parts of this lower level language execute. Certain patterns within the machine code select the Mov, Inc parts of the lower level language that is to execute.
+John Philip Jones So, there is a lower level language than machine code. What it is? It seems to be getting complicated, is there any website or book that gives a detailed information and help me understand?
+Simon Lee One of the levels below is microcode and this is responsible for directing binary patterns throughout the CPU. The main controlling subsystem within the CPU is the control unit. However, my advice is not to go below machine code as unless you are an electronic engineer it is more likely to confuse any understanding of how you can control a microprocessor system using machine code. My recommendation is to consider the register set of a typical CPU and use register transfer language (RTL) to understand how each machine code instruction executes. The best way is to look at manuals associated with a microprocessor (although they are not easy reads). A good starting place is to search for the "Z80 assembly language manual". Hope this helps Phil
Hi, I hope you can help me: I know what opcodes are and how they work, but i want to know something that i have just realised i've overlooked all this time, assembled code (binary after assembly) is just a sequence of instructions in the .text section right? (so all the opcodes with applicable operands). If so, i was wondering essentially how the CPU knows what each opcode actually refers to e.g. how does 0xDE mean 'load'? for example: say the cpu of a microcontroller turns on, the program counter = 0, so it loads the first opcode in .text (at the start of the section) into cpu and a hardware decoder in the cpu physically decodes each opcode which allows the CPU to translate the intended instruction into the correct operation, right? (then as program counter increments, you process the next operation (opcode)in .text). cheers!
+orbita1 Each OS has it's own standard of executable (Windows - PE, Linux - ELF...). OS loads the executable and parses the exe header and finds out starting address. OS loader then memory maps the exe and sets IP or EIP register to proper starting address by context switching. The program then continues there on. Now depending on addressing mode, the operand can be from .text part or .bss or .data? etc segment. But the first instruction (after switching on) is always a predefined address. For Intel/AMD IBM compatible pc, it is FFFFFFF0h or FFFF0h (depending on 32bit/16bit). Most of them are pointing to BIOS startup routines, which sets up rest of the booting process. Once the ball starts rolling, it works.
Your struggle may be related to asking how does the computer "understand." Perhaps it would help to remember that a computer is merely a bunch of switches. Some of those switches turn other switches on and off. The computer does not "understand" anything in the way a human understands things. The computer is just switches which have been arranged by humans in such a way that certain switches turn other switches on and off in a way to represent numbers and characters understandable to humans. A light switch does not "understand" that the up position means that it is been asked to light the room. When the switch is in the up position power flows to the light because humans have designed the circuit to work that way.
+sanaaf Each microprocessor will have it own user manual and there will be a list of assembly language instruction and in the column next to the instruction will be the hexadecimal value for the instruction. However, you would normally type up the program using an editor and use an assembler to convert the program to machine code (i.e. hexadecimal which is a way to represent binary). Regards Phil (John Philip Jones)
I have never coded for a super computer so cannot really comment. However, I used to code a lot in machine code. What I can say is that all languages are translated to machine code which then 'controls' the microcode (and/or the 'hardwiring'). With a FULL knowledge of the CPU you can always ensure a very efficient execution time when using machine code (well assembly language to be more accurate). However, it would take you an extremely long time to code any meaningful system using machine code. The C programming language has many efficient libraries for controlling CPUs and C programs can be much quicker to 'code up' than assembly language.
The first binary pattern (or first bits in the pattern) fetched fromm memory when you execute a program is always the opcode. Once decoded the instruction knows what to do with the rest of the instruction. This usually involve going back to the memory to get the operand. Once the current instruction has executed the CPU goes back to the memory to fetch the next instruction the and this will be the opcode.
You used the term "INC A" quite often... but when you showed the binary or hex representation of it, it doesn't seem to include the 'A' part... I'm guessing because it's A (the first register of the CPU), you just omit it, but when you want to access B or C, would you simply have an operand directly proceeding the 3D (INC) Op Code? (Tried to make it as simple as possible to understand, sorry)
I've been learning assembly for a while, but I had no clue that the destination register is part of the operator. I've been thinking of a and b as both being operands, but I guess it's not the case. Looks like people who use AT syntax are subtracting backwards =/
The chip manufacturers have manuals on instruction sets. I used the Z80 manual for the information in this video. Back in the day I used to program this chip using assembly language and machine code. Do a Google search on the following to get the manual. Zilog Z80 manual Best wishes Phil
Thank you for these tutorials... I am far more happier immersing myself in this type of learning activity than getting caught up watching political videos on UA-cam.. For old times sake I have just discovered and installed Turbo C on my PCs like I used in the 90s...
There is essentially a table that contains the mnemonics and opcode in there own column. The mnemonics (i.e. assembly language program) is supplied as a file to a computer program (the assembler) the table is referenced and an executable file that contains the opcodes and operands is created (this is the machine code). The machine code program is then loaded into the computers memory and this program is then executed
You would typically write a machine code program in assembly language. The assembly language is then converted to machine code and stored in a file. The file content are then loaded into RAM to be executed. It is a case of locating the op code of the first assembly language instruction. Every assembly language instruction has a number of bytes that form the opcode and operand. Once you find the first op code you can count the number of bytes to the start of the next instruction. This will be the op code of the next instruction. Best wishes Phil
There is always a fetch of the opcode which then needs to be decoded to find out what this instruction (i.e. Opcode) is for. After the decode you are in the execute phase. An execute phase does not always mean that the ALU is used. For example moving data from a register to a memory location is an example of an execute phase.
Yes the instructions are in the computers memory. They are fetched from the memory. See the video at the following link: www.johnphilipjones.com/video-fetch-decode-execute-cycle.html
Assembly language does offer some unique capabilities that higher-level languages might not provide. Here are a few key points: Direct Hardware Access: Assembly language allows you to interact directly with the hardware, giving you control over specific CPU instructions and memory addresses. This level of control is often necessary for writing device drivers or real-time systems. Performance Optimization: Because you can write instructions that are executed directly by the CPU, you can optimize your code for maximum performance. This is particularly useful in scenarios where every millisecond counts, such as in embedded systems or high-frequency trading applications. Minimal Overhead: Higher-level languages often introduce some overhead due to abstractions and additional layers of code. Assembly language, being closer to machine code, has minimal overhead, which can be crucial in resource-constrained environments. Learning and Debugging: Understanding assembly language can provide deeper insights into how computers work at a fundamental level. It can also be helpful for debugging, as you can see exactly what the CPU is doing at any given moment. However, it's worth noting that higher-level languages are generally more productive and easier to maintain, as they abstract away many of the complexities involved in programming at the hardware level. They also come with extensive libraries and frameworks that can speed up development.
@@tamimayan2028 Knowing assembly language can give you a deeper understanding of how software interacts with the hardware, and it can be incredibly powerful for low-level programming and system optimization. However, it doesn't necessarily mean that all software or programming languages will be open source or accessible to you on that specific computer. Here are a few key points to consider: 1. **Understanding Machine Code**: - Assembly language is a low-level programming language that is closely related to machine code. If you understand assembly, you can read and potentially modify machine code, which is the lowest level of code executed by the computer's CPU. 2. **Access to Source Code**: - Open-source software means that the source code is freely available for anyone to view, modify, and distribute. Understanding assembly language doesn't grant you access to closed-source software. If a program is closed-source, its source code is not publicly available regardless of your knowledge of assembly. 3. **Reverse Engineering**: - Knowledge of assembly can help you reverse engineer software, which means analyzing a program's binary code to understand its functionality. While this can be useful for debugging or security research, it's important to note that reverse engineering closed-source software can violate licensing agreements and intellectual property laws. 4. **Programming Language Interoperability**: - Understanding assembly language can help you write more efficient code in higher-level languages and understand how those languages are translated into machine instructions. However, each programming language has its own syntax and semantics, and being proficient in assembly doesn't automatically make you proficient in all other programming languages. In summary, knowing assembly language can significantly enhance your understanding of how software works at a fundamental level and help you with tasks like optimization and reverse engineering. However, it doesn't provide automatic access to all software or make every programming language open source. Respecting intellectual property and software licenses is crucial.
WOW, so easy! You've earned all your subs, and now you've got another one... ME! :) God bless you and thank you, have an amazing, prosperous life :)) Matthew, Northants.
7 years later and this video is relevant now more than EVER! top man Mr. Jones! can't thank you enough Sir!
Thank you.
Best wishes
Phil
Thank you for your generous comment.
Best wishes
Phil
I want to thank you for your video!!! I studied this long time ago, but it is wonderful you decided to post this video so more people can understand how a computer works!!!
Information and understanding it, is important in human development!!!!
THANK YOU
Thank you for your comment it is appreciated.
Best wishes
Phil
this helps me in understanding ethereum and how EVM bytecodes work. thanks a bunch!
Truly the most brilliant video series I have ever seen when it comes to explaining this topic. I can't believe how much I appreciate them. Thank you so much!
Thanks. My goal is to learn assembly in 2025. This and your othe rexplanations made my task a lot clearer.
It is good to know that the videos help.
Best wishes Phil
Probably the best explanation that i have ever seen , short , constructive , hit's brain directly , thank you very much !
This is by far the best way of teaching computer science! Thanks alot!
May I say... how GRATEFUL I am for your amazing videos
Thank you
you are literally amazing and finally, allowing me understand this topic for my ee class.
Thank you for your comment. I am concentrating on Python videos at the moment but will return to more on machine code late next year.
Best wishes
Phil
Very useful! I missed watching your videos. I like your explanation 👍🏼👍🏼
I really want to watch a video of how they got from fiddling with transistors by hand to something that can interpret instructions.
+Tim Crinion That would be a long duration video
Best
Phil
+Tim Crinion I just went through a 4 month 200 level class to learn that. It's not easy m8.
Yes it is like having a personal tutor. Excellent and thank you very much.
Thank you so much, here in Brazil there aren't video explanations like this.
Great stuff Scouse ! Super clear presentation. Hope other "teachers" gain your skill in clear communication. Keep up the good work !
Extremely clear.
Was looking for this kind of explanation for 5 hours! Thank you very much.
You replies are actually better than the video itself. Great information, Phil, nice one !
AWESOME video, just subbed! I’m a CS guy that loves the CE domain more than anything. This video has really helped me, getting ready to start writing a 6502 emulator. I will for sure check out your other videos as well. Cheers!
Thank you for the positive feedback it is appreciated.
Best wishes Phil
Thanks for sharing these useful tutorials. They helped me a lot understanding how does CPU work, and how can I understand Assembly.
Thank you so much! Your videos are very useful and easy to follow. please upload more!
Thank you for your comments I hope to upload two or three a week.
Bw
Phil
John phillip jones, this video is astounshing and really helped me. Really impeccable.
Thanks for this. You made something that I thought was complicated really, really simple.
Thank you for your comment
Bw
Phil
Man you realy helped no would could help me like this keep up the good work
I am not a CS major but rather an MIS major, it's cousin. I chose MIS to make getting an MBA easier in the future, but man, do I love coding from the bottom of my heart. It's awesome, and high level coding comes to me pretty naturally.
Even if I will never use it, I want to learn low level coding too. It's fascinating. Now if only I could understand it just a bit better >.>
Beautiful!
I want to one day program in binary. Literally. Even just a little.
Thank you for showing binary representations of some the assembly instructions.
Gosh! Finally a very good explanation about this topic. Thanks John!! You made my day!
informative, good and clear tutorial , please make more , thanks !
The lookup tables for getting from Assembly instructions to Opcodes - are these available / queryable from the OS?
You would need to install an assembler. To get the opcodes search on google for the instruction set of the processor.
Best wishes
Phil
Great Teaching.
I get how you know that 01H is 00000001 in binary... But how do you convert the LD A into binary? Or is it something your remember from a list? I have to convert it into machine code, is machine code binary?
This is a very good question. Machine code is the language of the machine (I.e low and high voltage levels) which as humans we represent as binary. Every machine code instruction has it's own binary number. Load A with immediate data has the binary number 00111110 which is 3E in hexadecimal (an easy way to remember binary). So every instruction has it's own binary number. Every instruction has it's own binary number so in a sense it comes from a list as you suggest. However, in truth each number makes the electronics behave in a specific way. The binary patterns open and close electronic solid state switches that perform a simple task that when all executed in a specific order are able to implement sophisticated functionality.
John Philip Jones So does LOAD have it's own binary number? Or for example do LOAD A, LOAD B and LOAD C count as three different things with three separate binary numbers representing them?
And does this mean that there is no way of mathematically converting LOAD A into binary in your head/on paper without a table that literally tells you which binary number represents each op code I think it's called?
malteeaser101 LD A, N has its own binary number called an Operation code. LD B, N has a different operation code. When writing assembly language program you type in instructions such as LD A, N and an assembler (computer program) converts the assembly language to machine code (i.e. binary patterns). You can also do the conversion as a human it is called hand assembly and it means you would have to either know all the codes or access them from a printed list.Back in the day I used to code in assembly and as sad as it may sound I knew all of the codes. But there was only around 70 different instructions for the machines I was working on.
John Philip Jones Haha thanks, I have a assessment due in soon and this helped A LOT.
Mo Fo Hello Mo, Every 'type' of CPU (processor) has its own machine code language and related assembly language. For example Intel processors are different to Motorola processors and have a different machine code language from one another. The description I have put forward is based on the Zilog Z80 processor which also has its own machine code language.
I am unsure what machine code language you are referring to but what you are describing is what is often called absolute addressing The term addressing refers to how data is accessed by an individual machine code instruction. I have a short series of videos that show the difference. I have put links to their playlist below:
ua-cam.com/play/PL6lxxT7IdTxGXX6D6j-zjzcgoOD8WYMS2.html
Phil
Nice work, thank you for your clear explanation.
sir ,is it true that increment operation is done in instruction decode phase only??
It is part of the execute phase
Phil
Thanks! You should do more videos on this topic as you explain this matter very, very well!
I will be doing more once I have finished doing the Python programming video
Best wishes
Phil
awesome tuto ,and it will be great if you can make a series about programming
in assembly (emulator 8086 or something like that )
and thanks
i'm lerning in chanel ser ...Thanks
you just saved me. Thanks a lot :D!
Good to hear that the video helped.
Best wishes Phil
awesome explanation thanx,and it will be great if you can make a video series about
programming using emulator 8086 or something like that
For Intel 80386+ CPU:
Instruction Prefix 0 or 1 Byte
Address-Size Prefix 0 or 1 Byte
Operand-Size Prefix 0 or 1 Byte
Segment Prefix 0 or 1 Byte
Opcode 1 or 2 Byte
Mod R/M 0 or 1 Byte
SIB, Scale Index Base (386+) 0 or 1 Byte
Displacement 0, 1, 2 or 4 Byte (4 only 386+)
Immediate 0, 1, 2 or 4 Byte (4 only 386+)
Format of Postbyte(Mod R/M from Intel-Doku)
------------------------------------------
MM RRR MMM
MM - Memory addressing mode
RRR - Register operand address
MMM - Memory operand address
RRR Register Names
Filds 8bit 16bit 32bit
000 AL AX EAX
001 CL CX ECX
010 DL DX EDX
011 Bl BX EBX
100 AH SP ESP
101 CH BP EBP
110 DH SI ESI
111 BH DI EDI
---
16bit memory (No 32 bit memory address prefix)
MMM Default MM Field
Field Sreg 00 01 10 11=MMM is reg
000 DS [BX+SI] [BX+SI+o8] [BX+SI+o16]
001 DS [BX+DI] [BX+DI+o8] [BX+DI+o16]
010 SS [BP+SI] [BP+SI+o8] [BP+SI+o16]
011 SS [BP+DI] [BP+DI+o8] [BP+DI+o16]
100 DS [SI] [SI+o8] [SI+o16]
101 DS [DI] [DI+o8] [SI+o16]
110 SS [o16] [BP+o8] [BP+o16]
111 DS [BX] [BX+o8] [BX+o16]
Note: MMM=110,MM=0 Default Sreg is DS !!!!
32bit memory (Has 67h 32 bit memory address prefix)
MMM Default MM Field
Field Sreg 00 01 10 11=MMM is reg
000 DS [EAX] [EAX+o8] [EAX+o32]
001 DS [ECX] [ECX+o8] [ECX+o32]
010 DS [EDX] [EDX+o8] [EDX+o32]
011 DS [EBX] [EBX+o8] [EBX+o32]
100 SIB [SIB] [SIB+o8] [SIB+o32]
101 SS [o32] [EBP+o8] [EBP+o32]
110 DS [ESI] [ESI+o8] [ESI+o32]
111 DS [EDI] [EDI+o8] [EDI+o32]
Note: MMM=110,MM=0 Default Sreg is DS !!!!
---
SIB is (Scale/Base/Index)
SS BBB III
Note: SIB address calculated as:
=+*(2^(Scale))
Fild Default Base
BBB Sreg Register Note
000 DS EAX
001 DS ECX
010 DS EDX
011 DS EBX
100 SS ESP
101 DS o32 if MM=00 (Postbyte)
SS EBP if MM00 (Postbyte)
110 SS ESI
111 DS EDI
Fild Index
III register Note
000 EAX
001 ECX
010 EDX
011 EBX
100 never Index SS can be 00
101 EBP
110 ESI
111 EDI
Fild Scale coefficient
SS =2^(SS)
00 1
01 2
10 4
11 8
Very informative! Thank you for the quality upload.
Thank you Mr John!!!!
Nicely done. But @5:54 the name “Immediate” actually refers to the operand being the actual data to operate on. As opposed to the operand being an address, or an offset, that points to the data (or further indirection). Specifically, “Immediate” does not mean the operand immediately follows the opcode, as you have stated. Clearly, the operand always immediately follows the opcode. The operand just isn’t always the “immediate” actual data to operate on.
What you are referring to is addressing modes. If the data to be processed by the instruction follows the opcode, then the addressing mode is referred to as immediate addressing. Another instruction can have the address of the data just after the opcode. This address gives the memory address of where the data to be processed can be located. This type of instruction is said to have an absolute addressing mode.
Immediate addressing is covered by the video at the link below:
ua-cam.com/video/C6P2s5J9RaM/v-deo.htmlsi=LuW6LxR811gcGlZ_
Absolute addressing is covered by the video at the link below:
ua-cam.com/video/L8RVT0zG8Zk/v-deo.htmlsi=EsyWTnOtZBb8ThUi
As an aside the addressing mode shown in the video at the link below is an example of implied addressing.
ua-cam.com/video/XM4lGflQFvA/v-deo.htmlsi=CwcaGDV0h8ju9Kso
Best wishes
Phil
@@johnphilipjones Hi Phil. I do understand all of that. I’m old school. I started off hand-assembling machine code on my own self-built 8-bit microprocessor designs in the late 70’s. So I do have a pretty solid understanding of all of this. I was just trying to give some helpful feedback for your students.
What I was pointing out is that @5:54 you specifically say: “the name immediate addressing comes from the fact that the operand comes immediately after the opcode”. This statement is potentially confusing to learners. i.e. The fact that the operand comes immediately after the opcode is a general statement, not specific to the addressing mode.
I believe what you meant to say is more along the lines of: “the name immediate addressing comes from the fact that the operand is the actual data to be processed by the opcode”. This would then be consistent with what you have now clarified in your reply above.
Hopefully you take this as the helpful constructive feedback I'd intended. 🤓
Hello Greg,
Yes you are correct I missed your point initially. I am always happy for feedback and comments. It was, and is, taken as constructive.
Best wishes
Phil
this was awesome thank you
very educative lesson ..sir can you please , prepare simple and nice video on VBA for beginners
So if I had for example machine code: 0xE1160547, the resulting assembly instruction would be the binary equivalent?
Yes that is correct
Best wishes
Phil
This video is fantastic. thanks for the lecture. thanks for posting it.
+Lo Janus Thank you for your generous comment.
Regards
Phil
(John Philip Jones)
Thank you very much for this very clear and precise explanation..
Very helpful. ... carry on plz
Great stuff! Thanks. Is it necessary to convert instructions and operands to hex numbers? Can you keep instructions as binary code instead of 3E or something like that?
+zinturis You would normally code in assembly language and this is converted to the necessary binary codes by the assembler. So in the machine you would actually see binary and not hexadecimal. However, it is usual to look at the binary in hexadecimal as it is easier for us to remember hexadecimal numbers rather than long binary numbers and it is also easy to convert between binary and hexadecimal. The following videos look at converting between Hexadecimal and binary:
ua-cam.com/video/fhgk2rxrn-Q/v-deo.html
ua-cam.com/video/V-he1Ip36OA/v-deo.html
Best wishes
Phil
(John Philip Jones)
Thanks a lot for the quick answer. Sorry if my questions may sound stupid, I am quite newbie in this but if you want to send certain instructions of machine code to the processor, what's the workflow of that? Can you do it from some terminal or do you need to create a text file and save as .bin or .. how that works in practice? I mean, how to control that process?
+zinturis You use an editor and type in your assembly language program and save it in a text file and use an assembler to convert your typed in program to machine code. You will need to download an assembler to match the CPU of your computer. However, a good way to learn is to download a CPU emulator that has an assembler incorporated. I used to do assembly programming on a Z80 flight micro-board, a 6511 micro-controller using an in-circuit emulator. However, these have now been superseded and I am not sure what is used today. However, I believe that the Raspberry Pi has a set up that allows for machine coding. I would recommend searching with the phrase "machine code emulator" and see what is out there to assist. Hope this helps.
Best wishes
Phil
(John Philip Jones)
From your earlier respond, I understand that assembly language is different for different CPUs? So, if I write z=x+y for Intel it is not gonna work on an AMD based computer? So what's different? Assembly commands or register addresses?
zinturis z = x + y is a high level statement and will be converted to the machine code of the CPU that is 'in' your computer by a compiler. However, putting this aside the opcode for add on each different type of CPU is different. Each processor also has a different register set i.e. different number of registers and of different lengths. The architecture of the CPU's can also be different some are RISC other CISC or a combination of both CISC and RISC.
Very nice. So we can either write out the assembler on a piece of paper, then convert on the paper and then maybe (in the early days) use switches to set each memory address with the correct sequence of binary bits ? The part I still get stuck on is that the assembler can also be a program to convert the codes for us. Is this a program that would be loaded into memory, and therefore the first assemlber would have had to have been hand written (from what I've read, but still a little confused )and manually placed in the memory ?
When you switch on a computer the electronics generates an electrical pulse. This pulse is applied to a CPU pin that causes a machine code instruction to be fetched from a specific memory location. This instruction is the first instruction of the boot program other instruction are then fetched decoded and executed in order one after another. This boot program then sets up the operating system by loading other programs that form the operating system. Once the operating system is in place it is able to load up other programs (such as the assembler) at the request of the user (i.e by clicking on an icon or by issuing a command).
In the very early days switches were indeed throw to enter the program you wished to execute.
VERY GOOD SIR.PLEASE UPLOAD FEW MORE VIDEOS
Check out www.PythonByteSize.com
Best wishes Phil
Hello, great video. My question is , are the registers hexadecimal form?
The registers actually hold binary patterns. It is usual to show these patterns in their hexadecimal form because as humans it is difficult for us to remember long binary patterns. It is much easier to remember hexadecimal and it is relatively easy to convert between binary and hexadecimal.
Best wishes Phil
Thank you sir for the video. But my question still is, how do you convert the assembly language code to machine code (opcode).Your reply will be much appreciated.
+Hammad Ahmed You write your program in assembly language and an assembler converts the assembly language to machine code.
Regards
Phil
(John Philip Jones)
Great video! Very visual! Keep it up!
Thank you for commenting Bw Phil
How are you doing sir, I liked this youtube very much sir, I am expected you are making these kind of, more sir
hey thanx for this tutorial but how did you find the opcode for the instructions like inc, add, mov and so on....
Danish Bhatia All microprocessors have an instruction set. This is a list of instructions in together with there hexadecimal equivalent. The link below will take you to an instruction set in PDF format which can be downloaded.
Danish Bhatia oops this is the link:
www.zilog.com/appnotes_download.php?FromPage=DirectLink&dn=UM0080&ft=User%20Manual&f=YUhSMGNEb3ZMM2QzZHk1NmFXeHZaeTVqYjIwdlpHOWpjeTk2T0RBdmRXMHdNRGd3TG5Ca1pnPT0=
I want to know about how machine code is executed. I mean, since you already have these 1s and 0s in RAM, how can the CPU tell from so many instructions and do different actions correspondingly?
+Simon Lee When you switch on a microprocessor system (e.g. a computer) the hardware will always look in the same memory location. This location must hold the first operation code to be executed i.e. the program to be executed starts at this memory location. For a computer this is the boot program. For an embedded system containing a microprocessor this location would contain the first operation code for the first program to be executed.
+John Philip Jones I am sorry, but I am still confused. I mean how the CPU can know these 1s or 0s mean MOV, ADD or SUB etc. How do 1s and 0s make sense to the CPU and indicate the CPU what to do?
+Simon Lee A microprocessor system is a collection of million of switches that control the movement of binary patterns along bus systems, In addition these switches can be arranged to perform logic and arithmetic operations. The control of these switches is achieved by another lower level language that is not machine code. However, the machine code is able to dictate which parts of this lower level language execute. Certain patterns within the machine code select the Mov, Inc parts of the lower level language that is to execute.
+John Philip Jones So, there is a lower level language than machine code. What it is? It seems to be getting complicated, is there any website or book that gives a detailed information and help me understand?
+Simon Lee One of the levels below is microcode and this is responsible for directing binary patterns throughout the CPU. The main controlling subsystem within the CPU is the control unit. However, my advice is not to go below machine code as unless you are an electronic engineer it is more likely to confuse any understanding of how you can control a microprocessor system using machine code. My recommendation is to consider the register set of a typical CPU and use register transfer language (RTL) to understand how each machine code instruction executes. The best way is to look at manuals associated with a microprocessor (although they are not easy reads). A good starting place is to search for the "Z80 assembly language manual".
Hope this helps
Phil
This video helps a lot. Thank you :)
Hi, I hope you can help me:
I know what opcodes are and how they work, but i want to know something that i have just realised i've overlooked all this time, assembled code (binary after assembly) is just a sequence of instructions in the .text section right? (so all the opcodes with applicable operands).
If so, i was wondering essentially how the CPU knows what each opcode actually refers to e.g. how does 0xDE mean 'load'?
for example: say the cpu of a microcontroller turns on, the program counter = 0, so it loads the first opcode in .text (at the start of the section) into cpu and a hardware decoder in the cpu physically decodes each opcode which allows the CPU to translate the intended instruction into the correct operation, right? (then as program counter increments, you process the next operation (opcode)in .text).
cheers!
+orbita1 I think I just have the same confusion like you. How can these 1s and 0s make sense to the CPU and be executed differently?
+orbita1 Each OS has it's own standard of executable (Windows - PE, Linux - ELF...). OS loads the executable and parses the exe header and finds out starting address. OS loader then memory maps the exe and sets IP or EIP register to proper starting address by context switching. The program then continues there on. Now depending on addressing mode, the operand can be from .text part or .bss or .data? etc segment.
But the first instruction (after switching on) is always a predefined address. For Intel/AMD IBM compatible pc, it is FFFFFFF0h or FFFF0h (depending on 32bit/16bit). Most of them are pointing to BIOS startup routines, which sets up rest of the booting process. Once the ball starts rolling, it works.
Your struggle may be related to asking how does the computer "understand." Perhaps it would help to remember that a computer is merely a bunch of switches. Some of those switches turn other switches on and off. The computer does not "understand" anything in the way a human understands things. The computer is just switches which have been arranged by humans in such a way that certain switches turn other switches on and off in a way to represent numbers and characters understandable to humans. A light switch does not "understand" that the up position means that it is been asked to light the room. When the switch is in the up position power flows to the light because humans have designed the circuit to work that way.
It old but it still so good
How we should find opcode for an operation is there any method r it is assigned value
+sanaaf Each microprocessor will have it own user manual and there will be a list of assembly language instruction and in the column next to the instruction will be the hexadecimal value for the instruction. However, you would normally type up the program using an editor and use an assembler to convert the program to machine code (i.e. hexadecimal which is a way to represent binary).
Regards
Phil
(John Philip Jones)
Can assembly language play a significant role in programming for the Super Computers?
I have never coded for a super computer so cannot really comment. However, I used to code a lot in machine code. What I can say is that all languages are translated to machine code which then 'controls' the microcode (and/or the 'hardwiring'). With a FULL knowledge of the CPU you can always ensure a very efficient execution time when using machine code (well assembly language to be more accurate). However, it would take you an extremely long time to code any meaningful system using machine code. The C programming language has many efficient libraries for controlling CPUs and C programs can be much quicker to 'code up' than assembly language.
Awesome! This helps so much with a personal project of mine (using logisim to simulate an 8 bit CPU)
Sir,how opcode and operands are combined in an instruction and how they are seperated in an instruction.
The first binary pattern (or first bits in the pattern) fetched fromm memory when you execute a program is always the opcode. Once decoded the instruction knows what to do with the rest of the instruction. This usually involve going back to the memory to get the operand. Once the current instruction has executed the CPU goes back to the memory to fetch the next instruction the and this will be the opcode.
@@johnphilipjones thank u sir and which digital circuits are invovlved in joining both opcode and operand together in an instruction.
would you be able to do a video on floating point binary?
I will put it in the pipeline.
Regards
phi
how to get opcode for different instruction set?
The following link takes you to the instruction set for the Z80: z80-heaven.wikidot.com/instructions-set
Best wishes
Phil
You used the term "INC A" quite often... but when you showed the binary or hex representation of it, it doesn't seem to include the 'A' part... I'm guessing because it's A (the first register of the CPU), you just omit it, but when you want to access B or C, would you simply have an operand directly proceeding the 3D (INC) Op Code?
(Tried to make it as simple as possible to understand, sorry)
INC B would have a different opcode. INC C would also have a different opcode
I've been learning assembly for a while, but I had no clue that the destination register is part of the operator. I've been thinking of a and b as both being operands, but I guess it's not the case. Looks like people who use AT syntax are subtracting backwards =/
With machine code I would recommend that you consider source and destination for the 'data items' (i.e. operands)
where do you get this information from
The chip manufacturers have manuals on instruction sets. I used the Z80 manual for the information in this video. Back in the day I used to program this chip using assembly language and machine code. Do a Google search on the following to get the manual.
Zilog Z80 manual
Best wishes Phil
what software do you use to produce the videos?
I have put this info at the following link:
www.pythonbytesize.com/educators.html
Best wishes
Phil
Thank you for these tutorials...
I am far more happier immersing myself in this type of learning activity than getting caught up watching political videos on UA-cam.. For old times sake I have just discovered and installed Turbo C on my PCs like I used in the 90s...
Very good video. You did a good job explaining this and the images shown really helps a lot. Thanks...
Thank you
Best wishes
Phil Jones
(John Philip Jones)
Helpful video.
Hi John - Are you using Z80 opcodes here ?
Yes
thank you it was very helpful :D !
The op codes we learning are only 4 bits and the remaining 12 bits are the address, can you explain this? please
What processor are you using?
Best wishes Phil
thanq sir..good work.... like to see ur more videos on youtube
Check out the following websites where I list all of the videos
www.johnphilipjones.com/index.html
www.pythonbytesize.com/
Best wishes
Phil
Very Nice :)
Good video
Can you tell me how assembler converts Mnemonics into opcode.
There is essentially a table that contains the mnemonics and opcode in there own column. The mnemonics (i.e. assembly language program) is supplied as a file to a computer program (the assembler) the table is referenced and an executable file that contains the opcodes and operands is created (this is the machine code). The machine code program is then loaded into the computers memory and this program is then executed
How does one find opcodes in an exe file?
You would typically write a machine code program in assembly language. The assembly language is then converted to machine code and stored in a file. The file content are then loaded into RAM to be executed. It is a case of locating the op code of the first assembly language instruction. Every assembly language instruction has a number of bytes that form the opcode and operand. Once you find the first op code you can count the number of bytes to the start of the next instruction. This will be the op code of the next instruction.
Best wishes
Phil
The great Z80!
Thank you Sir ❤❤
Most welcome
Best wishes Phil
But increment doesn't need alu operation,then why it is a part of execution cycle...
There is always a fetch of the opcode which then needs to be decoded to find out what this instruction (i.e. Opcode) is for. After the decode you are in the execute phase. An execute phase does not always mean that the ALU is used. For example moving data from a register to a memory location is an example of an execute phase.
Thank you so much sir
Hello Hamza,
I am developing videos on Python at the moment. But will add some on assembly late next year.
Best wishes
Phil
A instruction has to be in program memory, doesn't it?
Yes the instructions are in the computers memory. They are fetched from the memory. See the video at the following link: www.johnphilipjones.com/video-fetch-decode-execute-cycle.html
Great stuff exactly what I wanted to form good foundations
Cheers man
Thank you
Best wishes
Phil
Good old Z80 assembly.
Thank you so very much!
1.does assembly language allow you to do things you can’t in a higher level language?
2. Suggest me some books for assembly language
Assembly language does offer some unique capabilities that higher-level languages might not provide. Here are a few key points:
Direct Hardware Access: Assembly language allows you to interact directly with the hardware, giving you control over specific CPU instructions and memory addresses. This level of control is often necessary for writing device drivers or real-time systems.
Performance Optimization: Because you can write instructions that are executed directly by the CPU, you can optimize your code for maximum performance. This is particularly useful in scenarios where every millisecond counts, such as in embedded systems or high-frequency trading applications.
Minimal Overhead: Higher-level languages often introduce some overhead due to abstractions and additional layers of code. Assembly language, being closer to machine code, has minimal overhead, which can be crucial in resource-constrained environments.
Learning and Debugging: Understanding assembly language can provide deeper insights into how computers work at a fundamental level. It can also be helpful for debugging, as you can see exactly what the CPU is doing at any given moment.
However, it's worth noting that higher-level languages are generally more productive and easier to maintain, as they abstract away many of the complexities involved in programming at the hardware level. They also come with extensive libraries and frameworks that can speed up development.
@johnphilipjones is it true that if i know assembly language, every software/ programing language will be open source for me in that spacific computer
@@tamimayan2028 Knowing assembly language can give you a deeper understanding of how software interacts with the hardware, and it can be incredibly powerful for low-level programming and system optimization. However, it doesn't necessarily mean that all software or programming languages will be open source or accessible to you on that specific computer.
Here are a few key points to consider:
1. **Understanding Machine Code**:
- Assembly language is a low-level programming language that is closely related to machine code. If you understand assembly, you can read and potentially modify machine code, which is the lowest level of code executed by the computer's CPU.
2. **Access to Source Code**:
- Open-source software means that the source code is freely available for anyone to view, modify, and distribute. Understanding assembly language doesn't grant you access to closed-source software. If a program is closed-source, its source code is not publicly available regardless of your knowledge of assembly.
3. **Reverse Engineering**:
- Knowledge of assembly can help you reverse engineer software, which means analyzing a program's binary code to understand its functionality. While this can be useful for debugging or security research, it's important to note that reverse engineering closed-source software can violate licensing agreements and intellectual property laws.
4. **Programming Language Interoperability**:
- Understanding assembly language can help you write more efficient code in higher-level languages and understand how those languages are translated into machine instructions. However, each programming language has its own syntax and semantics, and being proficient in assembly doesn't automatically make you proficient in all other programming languages.
In summary, knowing assembly language can significantly enhance your understanding of how software works at a fundamental level and help you with tasks like optimization and reverse engineering. However, it doesn't provide automatic access to all software or make every programming language open source. Respecting intellectual property and software licenses is crucial.
can you explain this to me for 8086 instruction MOV CH,BL
Sorry I have never coded the 8086 so not sure what this instruction does.
Best wishes
Phil
yay! what a nice video
let me know if you have any questions
It really helps. Thank you.
Thanks a lot , Sir.
Brilliant
Thank you for your positive comment it is appreciated.
Best wishes
Phil
(John Philip Jones)
WOW, so easy! You've earned all your subs, and now you've got another one... ME! :) God bless you and thank you, have an amazing, prosperous life :))
Matthew, Northants.
Thank you for your generous comments.
Best wishes
Phil
Very good.
Thank you
Phil
thanks.