<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amrito Das Tipu</title>
    <description>The latest articles on DEV Community by Amrito Das Tipu (@amritoo).</description>
    <link>https://dev.to/amritoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F890640%2Fdf1366e3-6df2-4328-9460-cd2bec388b9e.png</url>
      <title>DEV Community: Amrito Das Tipu</title>
      <link>https://dev.to/amritoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amritoo"/>
    <language>en</language>
    <item>
      <title>A beginners guide to Assembly language using emu8086</title>
      <dc:creator>Amrito Das Tipu</dc:creator>
      <pubDate>Fri, 22 Jul 2022 04:05:43 +0000</pubDate>
      <link>https://dev.to/amritoo/a-beginners-guide-to-assembly-language-using-emu8086-2k75</link>
      <guid>https://dev.to/amritoo/a-beginners-guide-to-assembly-language-using-emu8086-2k75</guid>
      <description>&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is assembly language&lt;/li&gt;
&lt;li&gt;Assemblers and editors&lt;/li&gt;
&lt;li&gt;Code structure&lt;/li&gt;
&lt;li&gt;Registers and flags&lt;/li&gt;
&lt;li&gt;Assembly language instructions&lt;/li&gt;
&lt;li&gt;Working with variables&lt;/li&gt;
&lt;li&gt;Taking user input&lt;/li&gt;
&lt;li&gt;Displaying output&lt;/li&gt;
&lt;li&gt;Branching or using conditions&lt;/li&gt;
&lt;li&gt;
Using loops

&lt;ul&gt;
&lt;li&gt;For loop&lt;/li&gt;
&lt;li&gt;While loop&lt;/li&gt;
&lt;li&gt;Do-while loop&lt;/li&gt;
&lt;li&gt;Using LOOP syntax&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Include directive&lt;/li&gt;

&lt;li&gt;Extra: Reverse triangle problem&lt;/li&gt;

&lt;li&gt;Summary&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is assembly language
&lt;/h2&gt;

&lt;p&gt;Assembly language is a low-level programming language that is very fast, uses fewer resources compared to higher-level languages, and can be executed by translating directly to &lt;em&gt;machine language&lt;/em&gt; via an &lt;em&gt;assembler&lt;/em&gt;. According to &lt;a href="https://en.wikipedia.org/wiki/Assembly_language" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer programming, assembly language is any low-level programming language with a very strong correspondence between the instructions in the language and the architecture's machine code instructions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We know that a processor (also known as CPU - Central Processing Unit) executes all types of operations, effectively working as the brain of a computer. However, it only recognizes strings of 0's and 1's. As you can imagine, it's cumbersome to code in machine language. So, the low-level assembly language was designed for a specific family of processors that represents various instructions in symbolic code which is far easier to understand for a human being. But, as you can also guess, it's difficult and somewhat inconvenient to develop in assembly language.&lt;/p&gt;

&lt;p&gt;So, why should we learn assembly language in today's world?&lt;/p&gt;

&lt;p&gt;Well, you can think of the following points to decide whether to learn it or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enhance your skill set.&lt;/li&gt;
&lt;li&gt;Learn the fastest language aside from machine language.&lt;/li&gt;
&lt;li&gt;Embed assembly language in a higher-level language to use features unsupported by the higher-level language or for performance reasons.&lt;/li&gt;
&lt;li&gt;Fill in the knowledge gap for understanding how the higher-level languages came to be.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Assemblers and editors
&lt;/h2&gt;

&lt;p&gt;Assemblers are programs that translate assembly language code to its equivalent machine language code. There are many assemblers targeting various microprocessors in the market today like MASM, TASM, NASM, etc. For a list of different assemblers, visit &lt;a href="https://en.wikipedia.org/wiki/Comparison_of_assemblers" rel="noopener noreferrer"&gt;this Wikipedia page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Code editors are software in which you can write the code, modify and save it to a file. Some editors that support assembly language are VS code, DOSBox, emu8086, and so on. Online assemblers are also available, like the popular online editor &lt;a href="https://ideone.com/l/assembler-64bit-nasm" rel="noopener noreferrer"&gt;Ideone&lt;/a&gt;. We will use &lt;strong&gt;emu8086&lt;/strong&gt;, which comes with the environment needed to start our journey in assembly language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code structure
&lt;/h2&gt;

&lt;p&gt;We can simply write the assembly code and emulate it in emu8086, and it'll run. However, without calling the exit statements or &lt;code&gt;halt&lt;/code&gt; instruction, the program will continue executing the next instruction in memory until it is halted by OS or emu8086 itself. The assembly code is saved in a &lt;code&gt;.asm&lt;/code&gt; file type.&lt;/p&gt;

&lt;p&gt;There are also some good practices like defining the model and stack memory size at the very beginning. For &lt;code&gt;small&lt;/code&gt; model, define &lt;em&gt;data&lt;/em&gt; and &lt;em&gt;code&lt;/em&gt; segment after the stack. The &lt;em&gt;code&lt;/em&gt; segment contains the code to execute. In the example structure given here, I have created a &lt;code&gt;main&lt;/code&gt; procedure (also called function or methods in other programming languages), in which the code execution starts. At the end of it, I have called a specific predefined statement with interrupt to indicate the code has finished executing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.model small
.stack 100H

; Data segment
.data   ; if there is nothing in the data segment, you can omit this line.

; Code segment
.code

main PROC
    ; Write your code here

    exit:
    MOV AH, 4CH
    INT 21H
    main ENDP
END main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line, &lt;code&gt;.model small&lt;/code&gt;, defines the memory model to use. Some recognized memory models are tiny, small, medium, compact, large, and so on. The &lt;code&gt;small&lt;/code&gt; memory model supports one data segment and one code segment that are usually enough to write small programs. The following line &lt;code&gt;.stack 100H&lt;/code&gt; defines the stack size in hexadecimal numbers. The equivalent decimal number is &lt;code&gt;256&lt;/code&gt;. The lines starting with, or part of the line after, &lt;code&gt;;&lt;/code&gt; are comments that the assembler ignores.&lt;/p&gt;

&lt;h2&gt;
  
  
  Registers and flags
&lt;/h2&gt;

&lt;p&gt;Registers are superfast memory directly connected to the CPU. The emu8086 can emulate all internal registers of the &lt;em&gt;Intel 8086&lt;/em&gt; microprocessor. All of these registers are 16-bit long and grouped into several categories as follows,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;General purpose registers:&lt;/strong&gt; There are four general purpose registers, each divided into two subgroups, low and high. For example, AX is divided into AL and AH, each 8-bit long.

&lt;ul&gt;
&lt;li&gt;Accumulator (&lt;em&gt;AX&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Base (&lt;em&gt;BX&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Counter (&lt;em&gt;CX&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Data (&lt;em&gt;DX&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Segment registers:&lt;/strong&gt; There are also four segment registers.

&lt;ul&gt;
&lt;li&gt;Code Segment (&lt;em&gt;CS&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Data Segment (&lt;em&gt;DS&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Stack Segment (&lt;em&gt;SS&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Extra Segment (&lt;em&gt;ES&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Special purpose registers:&lt;/strong&gt; There are two index registers and three pointer registers.

&lt;ul&gt;
&lt;li&gt;Source Index (&lt;em&gt;SI&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Destination Index (&lt;em&gt;DI&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Base Pointer (&lt;em&gt;BP&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Stack Pointer (&lt;em&gt;SP&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Instruction Pointer (&lt;em&gt;IP&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Flag register:&lt;/strong&gt; This is a 16-bit register of which 9 bits are used by 8086 to indicate current state of the processor. The nine flags are categorized into two groups.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Status flags:&lt;/strong&gt; Six status flags indicate the &lt;em&gt;status&lt;/em&gt; of currently executing instruction.&lt;/li&gt;
&lt;li&gt;Carry flag (&lt;em&gt;CF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Parity flag (&lt;em&gt;PF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Auxiliary flag (&lt;em&gt;AF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Zero flag (&lt;em&gt;ZF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Sign flag (&lt;em&gt;SF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Overflow flag (&lt;em&gt;OF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control flags:&lt;/strong&gt; There are three control flags that controls certain operations of the processor.&lt;/li&gt;
&lt;li&gt;Interrupt flag (&lt;em&gt;IF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Direction flag (&lt;em&gt;DF&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Trap flag (&lt;em&gt;TF&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;To read more about these registers and what they are used for, &lt;a href="https://www.electronicsmind.com/registers-in-8086-microprocessor/" rel="noopener noreferrer"&gt;visit this page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Assembly language instructions
&lt;/h2&gt;

&lt;p&gt;A total of 116 instructions are available for the Intel 8086 microprocessor. All these instructions with related examples are &lt;a href="https://jbwyatt.com/253/emu/8086_instruction_set.html" rel="noopener noreferrer"&gt;provided in this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, I'll focus only on a few instructions necessary for understanding the later parts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Copy data (&lt;em&gt;MOV&lt;/em&gt;):&lt;/strong&gt; This instruction copies a byte (8-bit) or a word (16-bit) from source to destination. Both operands should be of the same type (byte or word). The syntax of this instruction is:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  MOV destination, source
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;destination&lt;/code&gt; operand can be any register or a memory location, whereas the &lt;code&gt;source&lt;/code&gt; operand can be a register, memory address, or a constant/immediate value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Addition (&lt;em&gt;ADD&lt;/em&gt;) and Subtraction (&lt;em&gt;SUB&lt;/em&gt;):&lt;/strong&gt; &lt;em&gt;ADD&lt;/em&gt; adds the data of the &lt;code&gt;destination&lt;/code&gt; and &lt;code&gt;source&lt;/code&gt; operand and stores the result in &lt;code&gt;destination&lt;/code&gt;. Both operands should be of the same type (words or bytes), otherwise, the assembler will generate an error. The subtraction instruction subtracts the &lt;code&gt;source&lt;/code&gt; from &lt;code&gt;destination&lt;/code&gt; and stores the result in &lt;code&gt;destination&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ; Addition
  ADD destination, source
  ADD BL, 10

  ; Subtraction
  SUB destination, source
  SUB BL, 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Label:&lt;/strong&gt; A label is a symbolic name for the address of the instruction that is given immediately after the label declaration. It can be placed at the beginning of a statement and serve as an instruction operand. The &lt;code&gt;exit:&lt;/code&gt; used before is a label. Labels are of two types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbolic Labels:&lt;/strong&gt; A symbolic label consists of an identifier or symbol followed by a colon (&lt;code&gt;:&lt;/code&gt;). They must be defined only once as they have global scope and appear in the object file's symbol table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Numeric Labels:&lt;/strong&gt; A numeric label consists of a single digit in the range zero (&lt;code&gt;0&lt;/code&gt;) through nine (&lt;code&gt;9&lt;/code&gt;) followed by a colon (&lt;code&gt;:&lt;/code&gt;). They are used only for local reference and excluded in the object file's symbol table. Hence, they have a limited scope and can be re-defined repeatedly.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ; Symbolic label
  label:
  MOV AX, 5

  ; Numeric label
  1:
  MOV AX, 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compare (CMP):&lt;/strong&gt; This instruction takes two operands and subtracts one from the other, then sets OF, SF, ZF, AF, PF, and CF flags accordingly. The result is not stored anywhere.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  CMP operand1, operand2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;operand1&lt;/code&gt; operand can be a register or memory address, and &lt;code&gt;operand2&lt;/code&gt; can be a register, memory, or immediate value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Jump instructions:&lt;/strong&gt; The jump instructions transfer the program control to a new set of instructions indicated by the label provided as an operand. There are two types of jump instructions.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unconditional jump (JMP):&lt;/strong&gt; It directly jumps to the provided label.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conditional jump:&lt;/strong&gt; These instructions are used to jump only if a condition is satisfied and called after &lt;code&gt;CMP&lt;/code&gt; instruction. This instruction first evaluates if the condition is satisfied through flags, then jumps to the label given as operand. It is pretty similar to &lt;code&gt;if&lt;/code&gt; statements in other programming languages. There are 31 conditional jump instructions available in 8086 assembly language.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Working with variables
&lt;/h2&gt;

&lt;p&gt;In an assembly program, all variables are declared in the &lt;code&gt;data&lt;/code&gt; segment. The emu8086 provides some &lt;em&gt;define directives&lt;/em&gt; for declaring variables. Specifically, we'll use &lt;code&gt;DB&lt;/code&gt; (define byte) and &lt;code&gt;DW&lt;/code&gt; (define word) directives in this article which allocates 1 byte and 2 bytes respectively.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[variable-name] define-directive initial-value [,initial-value]...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;variable-name&lt;/code&gt; is the identifier for each storage space. The assembler associates an offset value for each variable name defined in the data segment.&lt;/p&gt;

&lt;p&gt;Following is an example of variable declaration, where we initialize &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;char&lt;/code&gt; with a value that can be changed later. The &lt;code&gt;output&lt;/code&gt; is initialized with a string and has a dollar symbol (&lt;code&gt;$&lt;/code&gt;) at the end to indicate the end of string. The &lt;code&gt;input_char&lt;/code&gt; is declared without any initial value. We can use &lt;code&gt;?&lt;/code&gt; to indicate that the value is currently unknown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Data segment
.data
num DB 31H
char DB 'A'
output DW "Hello, World!!$"
input_char DB ?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We cannot use the variables in the &lt;em&gt;code&lt;/em&gt; segment just yet! For using these variables in the &lt;em&gt;code&lt;/em&gt; segment, we have to first move the address of the &lt;em&gt;data&lt;/em&gt; segment to the &lt;code&gt;DS&lt;/code&gt; (data segment) register. Use this line at the beginning of the &lt;em&gt;code&lt;/em&gt; segment to import all variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Storing all variables in data segment
MOV AX, @data
MOV DS, AX
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Taking user input
&lt;/h2&gt;

&lt;p&gt;The emu8086 assembler supports user input by setting a predefined value &lt;code&gt;01&lt;/code&gt; or &lt;code&gt;01H&lt;/code&gt; in the &lt;code&gt;AH&lt;/code&gt; register and then calling interrupt (&lt;code&gt;INT&lt;/code&gt;). It will take a single character from the user and save the &lt;a href="https://en.wikipedia.org/wiki/ASCII" rel="noopener noreferrer"&gt;ASCII&lt;/a&gt; value of that character in the &lt;code&gt;AL&lt;/code&gt; register. The emu8086 emulator displays all values in hexadecimal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; input a character from user
MOV AH, 1
INT 21h   ; the input will be stored in AL register
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Displaying output
&lt;/h2&gt;

&lt;p&gt;The emu8086 supports single character output. It also allows multi-character or string output. Similar to taking input, we have to provide a predefined value in the &lt;code&gt;AH&lt;/code&gt; register and call interrupt. The predefined value for single character output is &lt;code&gt;02&lt;/code&gt; or &lt;code&gt;02H&lt;/code&gt; and for string output &lt;code&gt;09&lt;/code&gt; or &lt;code&gt;09H&lt;/code&gt;. The output value must be stored in the general-purpose data register before calling interrupt.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Output a character
MOV AH, 2
MOV DL, 35
INT 21H

; Output a string
MOV AH, 9
LEA DX, output
INT 21H
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As shown in the code, for a single character output, we store the value in the &lt;code&gt;DL&lt;/code&gt; register because a character is one byte or 8 bits long. However, for string output it is a bit different. We must load the effective address (address with offset) of the string variable in the &lt;code&gt;DX&lt;/code&gt; register using &lt;code&gt;LEA&lt;/code&gt; instruction. The string variable must be defined in data segment.&lt;/p&gt;

&lt;p&gt;The complete code containing variable declaration, input and output is provided in &lt;a href="https://github.com/amritoo/assembly-codes/blob/main/variable_and_io.asm" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Branching or using conditions
&lt;/h2&gt;

&lt;p&gt;We can simulate if-else conditions supported by higher-level programming languages using &lt;code&gt;CMP&lt;/code&gt; and jump instructions. Some frequently used conditional jump instructions are,&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Instruction&lt;/th&gt;
&lt;th&gt;Jump if&lt;/th&gt;
&lt;th&gt;Similar to&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JE&lt;/td&gt;
&lt;td&gt;equal&lt;/td&gt;
&lt;td&gt;==&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JL&lt;/td&gt;
&lt;td&gt;less&lt;/td&gt;
&lt;td&gt;&amp;lt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JLE&lt;/td&gt;
&lt;td&gt;less than or equal&lt;/td&gt;
&lt;td&gt;&amp;lt;=&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JG&lt;/td&gt;
&lt;td&gt;greater&lt;/td&gt;
&lt;td&gt;&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JGE&lt;/td&gt;
&lt;td&gt;greater than or equal&lt;/td&gt;
&lt;td&gt;&amp;gt;=&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is also &lt;code&gt;JMP&lt;/code&gt; instruction that works similar to &lt;code&gt;else&lt;/code&gt; statements found in higher-level languages. Following is an assembly code that compares &lt;code&gt;AL&lt;/code&gt; register value to &lt;code&gt;5&lt;/code&gt; and sets an appropriate value in the &lt;code&gt;BL&lt;/code&gt; register.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; setting a test value
MOV AL, 5

; Compare
CMP AL, 5
JG greater  ; if greater
JE equal    ; else if equal
JMP less    ; else

greater:
MOV BL, 'G'
JMP after

equal:
MOV BL, 'E'
JMP after

less:
MOV BL, 'L'

after:
; Other codes
; Note: BL will contain 'E' at this point
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A complete code is available in this &lt;a href="https://github.com/amritoo/assembly-codes/blob/main/condition.asm" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using loops
&lt;/h2&gt;

&lt;p&gt;We can also use loops in assembly language. However, unlike higher-level language, it does not provide different loop types. Though, the emu8086 emulator supports five types of loop syntax, &lt;code&gt;LOOP&lt;/code&gt;, &lt;code&gt;LOOPE&lt;/code&gt;, &lt;code&gt;LOOPNE&lt;/code&gt;, &lt;code&gt;LOOPNZ&lt;/code&gt;, &lt;code&gt;LOOPZ&lt;/code&gt;, they are not flexible enough for many situations. We can create our self-defined loops using condition and jump statements. Following are various types of loops implemented in assembly language, all of which are equivalent.&lt;/p&gt;

&lt;h3&gt;
  
  
  For loop
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;for&lt;/em&gt; loop has an initialization section where loop variables are initialized, a loop condition section, and finally, an increment/decrement section to do some calculation or change loop variables before the next iteration. Following is an example &lt;em&gt;for&lt;/em&gt; loop in &lt;code&gt;C&lt;/code&gt; language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;bl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;cl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// body&lt;/span&gt;
  &lt;span class="n"&gt;bl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The equivalent assembly code is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV BL, '0'

init_for:
; initialize loop variables
MOV CL, 0

for:
; condition
CMP CL, 5
JGE outside_for

; body
INC BL

; increment/decrement and next iteration
INC CL
JMP for

outside_for:
; other codes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  While loop
&lt;/h3&gt;

&lt;p&gt;Unlike &lt;em&gt;for&lt;/em&gt; loop, &lt;em&gt;while&lt;/em&gt; loop has no initialization section. It only has a loop condition section, which if satisfied, executes the body part. In the body part, we can do some calculations before the next iteration. Following is an example &lt;em&gt;while&lt;/em&gt; loop in &lt;code&gt;C&lt;/code&gt; language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;bl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// body&lt;/span&gt;
  &lt;span class="n"&gt;bl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;cl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The identical assembly code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV CL, 0
MOV BL, '0'

while:
; condition
CMP CL, 5
JGE outside_while

; body
INC BL
INC CL

; next iteration
JMP while

outside_while:
; other codes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Do-while loop
&lt;/h3&gt;

&lt;p&gt;Similar to the &lt;em&gt;while&lt;/em&gt; loop, the &lt;em&gt;do-while&lt;/em&gt; loop has a loop condition section and body. The only difference is that the code in the body executes at least once, even if the condition evaluates to &lt;code&gt;false&lt;/code&gt;. Following is an example &lt;em&gt;do-while&lt;/em&gt; loop in &lt;code&gt;C&lt;/code&gt; language.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;bl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// body&lt;/span&gt;
  &lt;span class="n"&gt;bl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;cl&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cl&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The matching assembly code is as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV CL, 0
MOV BL, '0'

do_while:
; body
INC BL
INC CL

; condition
CMP CL, 5
JL do_while

; other codes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using LOOP syntax
&lt;/h3&gt;

&lt;p&gt;We can use predefined loop syntax using the &lt;code&gt;CX&lt;/code&gt; register as a counter. Following is an example of loop syntax, which does the same thing as previous loops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MOV BL, '0'

; initialize counter
MOV CX, 5

loop1:
INC BL
LOOP loop1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A complete code containing various loops are available in &lt;a href="https://github.com/amritoo/assembly-codes/blob/main/loops.asm" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Include directive
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Include&lt;/em&gt; directive is used to access and use procedures and macros defined in other files. The syntax is &lt;code&gt;include&lt;/code&gt; followed by a file name with an extension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;include file_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The assembler automatically searches for the file in two locations and shows an error if it cannot find it. The locations are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The folder where the source file is located&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Inc&lt;/code&gt; folder&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the &lt;em&gt;Inc&lt;/em&gt; folder, there is a file &lt;strong&gt;emu8086.inc&lt;/strong&gt;, which defines some useful procedures and macros that can make coding easier. We have to include the file at the beginning of our source code to use these functionalities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;include 'emu8086.inc'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can use these macros in the code segment. Some of these macros and procedures that I find most useful are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;PRINT&lt;/em&gt; macro to print a string. Example usage: &lt;code&gt;PRINT output&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;PUTC&lt;/em&gt; macro to print an ASCII character. Example usage: &lt;code&gt;PUTC char&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;GET_STRING&lt;/em&gt; procedure to get a null-terminated string from a user until the &lt;code&gt;Enter&lt;/code&gt; key is pressed. Declare &lt;code&gt;DEFINE_GET_STRING&lt;/code&gt; before the &lt;code&gt;END&lt;/code&gt; directive to use this procedure.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;CLEAR_SCREEN&lt;/em&gt; procedure to clear the entire screen and set the cursor position to the beginning. Declare &lt;code&gt;DEFINE_CLEAR_SCREEN&lt;/code&gt; before the &lt;code&gt;END&lt;/code&gt; directive to use this procedure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To learn more about the macros and procedures inside the &lt;code&gt;emu8086.inc&lt;/code&gt; file &lt;a href="https://jbwyatt.com/253/emu/asm_tutorial_05.html" rel="noopener noreferrer"&gt;visit this page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extra: Reverse triangle problem
&lt;/h2&gt;

&lt;p&gt;Let's solve a problem that uses all that we learned so far. The task is to input a number (1-9) from the user and print a reverse triangle shape using &lt;code&gt;#&lt;/code&gt; in the console. Also, appropriate error messages should be displayed, if the user inputs an invalid character. A demo output shown in the image.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6oj6z71pe2dl5utqr6c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6oj6z71pe2dl5utqr6c.png" alt="Ireverse triangle example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try it yourself first and if you cannot solve it, then read on.&lt;/p&gt;

&lt;p&gt;To solve this problem, we have to do the following tasks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input a number from the user&lt;/li&gt;
&lt;li&gt;Validate the input&lt;/li&gt;
&lt;li&gt;Display user-friendly messages&lt;/li&gt;
&lt;li&gt;Now comes the tricky part. We cannot use a single for loop to print a reverse triangle shape. For this, we have to use two loops one inside the other, also known as &lt;em&gt;nested loops&lt;/em&gt;. In the outer loop, we can check how many lines are to be printed and also print the new line at the beginning or the end. The inner-loop can be used to print &lt;code&gt;#&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following is a demo code for the nested loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Initialize outer loop counter
MOV BL, 0   ; counts line number starting from 0

outer_loop: ; using while loop format
CMP BL, x   ; assuming x contains user input
JE outside_loop

; Print new-line

; Initialize inner loop counter
MOV CH, 0
MOV CL, x
SUB CL, BL  ; subtract current line number from x

inner_loop:
; Print #

LOOP inner_loop

; Increment outer loop counter
INC BL
JMP outer_loop

outside_loop:
; other codes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final output of my code is as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulkxgl4vqhnp8k2gkokv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulkxgl4vqhnp8k2gkokv.png" alt="reverse triangle problem output screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The complete solution is available in my &lt;a href="https://github.com/amritoo/assembly-codes/blob/main/reverse_triangle.asm" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;We covered so many contents in this article. First, we understood what assembly language is and some assemblers' names. Then, we understood a code structure and discovered all the registers and flags in the 8086 microprocessor. After comprehending some assembly instructions, we learned how to define a variable, how to take input from the user, and also how to output something on the screen. Then we learned about conditions and loops, and finally, to wrap up, we solved a problem using assembly language.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>assembly</category>
      <category>emu8086</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
