MPLab1 Addition
Jump to navigation
Jump to search
32-bit Binary Addition Step by Step
1. Specify the memory model for code, data, and stack segments
.model small .stack 100H .386 ; This specifies that the program will use instructions of the 80386 processor (32-bit instructions).
2. Define your data in the data section ( here you can define various data items: variables, constants, strings, arrays)
.data ; .data directive indicates the beginning of data section data1 dd 00H ; declares a double-world variable and initializes it to 0. num1 db 10,13,"Enter the first number: $" num2 db 10,13,"Enter the second number: $" sum db 10,13,"The sum is :$"
3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)
.code ; .code directive indicates the beginning of code section .startup ; the entry point of the program
MOV EBX, 00000000 ; Initialize EBX to 0; EBX will be used to store the sum.
; Display the "Enter the first number: " message using DOS interrupt 21h (AH=09h). MOV AH,09 MOV DX,OFFSET num1 INT 21H
MOV ECX, 8 ; initialize ECX to 8, to read 8 characters
AGAIN: MOV AH, 01 ; Read a character from the input device using DOS interrupt 21h (AH=01h). AGAIN is label for loop. INT 21H CMP AL, 'A' ; Check if the character is greater than or equal to 'A' JGE P1 ; If greater than 'A', jump to P1 SUB AL,30H ; subtract 30H to convert it to a decimal digit. JMP P4 P1: SUB AL, 37H ; subtract 37H to convert it to a decimal digit. P4: SHL EBX, 4 ; Shift the value in EBX left by 4 bits MOV AH,00 ADD EBX, EAX LOOP AGAIN MOV data1, EBX
The Assembly Program Structure
An assembly program is separated into three sections as follows:
.MODEL SMALL .STACK 100h
.DATA ;data definition go here
.CODE ;instructions go here
Steps to run Assembly programs in TASM
- Edit the program using any editor(gedit, notepad, etc.) and save the assembly code with .asm extension (e.g myCode.asm) in the same directory where you installed TASM and TLINK .
- Run TASM myCode.asm - it will invoke Turbo Assembler (TASM) which reads the "myCode.asm" file, processes the assembly instructions, and generates an object file or a binary executable file.
- Run TLINK myCode.obj - it will invoke Turbo Linker which reads the "myCode.obj" file, processes any external symbols and references, and combines it with other object files or libraries if needed. The result of this process is an executable program that you can run on your computer.
- Run myCode.exe - Once the linking process is successful, you can run your program by typing its name.
- Run TD myCode.exe - Debug (if needed)