Skip to content

Instantly share code, notes, and snippets.

@geongeorge
Last active May 3, 2019 02:36
Show Gist options
  • Save geongeorge/428b1af3fa2e80b313c704410dd435d9 to your computer and use it in GitHub Desktop.
Save geongeorge/428b1af3fa2e80b313c704410dd435d9 to your computer and use it in GitHub Desktop.
MASM x86 Assembly Programs

Notes

Print a Message

.model small
.stack 100h
.data
output db "Hello, World!"

.code
start:
	mov ax, @data
	mov dx, ax
	lea dx, output	;load effective address of output to dx
	mov ah, 09h	;print interrupt
	int     21h	;call interrupt
end start

Input a Char/Number

mov ah,01h        ;input number
int 21h
;Value in al
sub al,30h        ;[optional] If needed to convert to number

Print a Char/Number

mov dl,bl         ;mov to dl to print
add dl,30h	  ;[optional] If needed to convert to number
mov ah,02h        ;print what's in dl
int 21h

Division

div bl            ;div ax by bl
;remainder -> dx
;quotient -> ax

Division in x86

Variables

;Define variable
a dw ?

;Save to variable
lea DI,a          ;load address of a to index reg
mov [DI],bl       ;save bl to location of b (a = bl)

;Access variables
mov ax,a 

Loop

MOV	CL, 10
L1:
;<LOOP-BODY>
DEC	CL
JNZ	L1

;OR

mov CX,10
l1:
;<loop body>
loop l1

Full Notes

Full PDF: MM Lab

Add 2 numbers

.model small
.stack 100h
.data
    a dw ?
    b dw ?
    m1 db "enter the first number: $"
    m2 db 0ah,0dh,"enter the second number:$"
    m3 db 0ah,0dh,"sum is:$"
.code
start:
    mov ax,@data
    mov ds,ax

    ;TO INPUT NUMBER ONE

    lea dx,m1        ;print message one
    mov ah,09h
    int 21h

    mov ah,01h        ;input 10s digit of number one
    int 21h
    sub al,30h        ;convert to number
    mov bl,0ah        ;mov 10 to bl
    mul bl            ;multiply 10 to al
    mov bl,al         ;copy to bl

    mov ah,01h        ;input 1s digit of number one
    int 21h
    sub al,30h        ;convert to number
    add bl,al         ;add to bl

    lea DI,a          ;load address of a to index reg
    mov [DI],bl       ;save bl to location of b (a = bl)


    lea dx,m2        ;print message two
    mov ah,09h
    int 21h

    ;TO INPUT NUMBER TWO

    mov ah,01h        ;input 10s digit of number two
    int 21h
    sub al,30h        ;convert to number
    mov bl,0ah        ;mov 10 to bl
    mul bl            ;multiply 10 to al
    mov bl,al         ;copy to bl

    mov ah,01h        ;input 1s digit of number two
    int 21h
    sub al,30h        ;convert to number
    add bl,al         ;add to bl

    lea DI,b          ;load address of b to index reg
    mov [DI],bl       ;save bl to location of b (b = bl)
    
    ;TO PRINT RESULT
    lea dx,m3         ;load effective address of add to dx
    mov ah,09h        ;print result message
    int 21h
    mov ax,000h
    ;ACTUAL ADDITION
    mov ax,a          ;mov a to ax
    mov bx,b          ;mov b to bx
    add ax,bx         ;add bl to al
    mov bl,0ah        ;save 10 to bx
    mov dx,0000h      ;clear
    mov cx,0000h      ;clear
    div bl            ;div ax by bx
    
    mov cx,dx         ;save dx(remainder) in bx because dx overwritten in printing

    add al,30h        ;add 30 to convert to ascii
    mov dl,al         ;mov to ax to print
    mov cl,ah
    mov ah,02h        ;print quotient
    int 21h

    mov dl,cl         ;mov to bx to print
    add dl,30h
    mov ah,02h        ;print remainder
    int 21h

    mov ah,4ch        ;return control to the terminal
    int 21h
end start

Calculator

.model small
.stack 100h
.data
	a dw ?
	b dw ?
	m1 db "enter the first number: $"
	m2 db 0ah,0dh,"enter the second number:$"
	m3 db 0ah,0dh,"sum is:$"
	m4 db 0ah,0dh,"difference is:$"
	m5 db 0ah,0dh,"product is:$"
	m6 db 0ah,0dh,"quotient is:$"
.code
start:
	mov ax,@data
	mov ds,ax

	;TO INPUT NUMBER ONE

	lea dx,m1		;print message one
	mov ah,09h
	int 21h

	mov ah,01h		;input 10s digit of number one
	int 21h
	sub al,30h		;convert to number
	mov bl,0ah		;mov 10 to bl
	mul bl			;multiply 10 to al
	mov bl,al		;copy to bl

	mov ah,01h		;input 1s digit of number one
	int 21h
	sub al,30h		;convert to number
	add bl,al		;add to bl

	lea DI,a	 	;load address of a to index reg
	mov [DI],bl		;save bl to location of b (a = bl)


	lea dx,m2		;print message two
	mov ah,09h
	int 21h

	;TO INPUT NUMBER TWO

	mov ah,01h		;input 10s digit of number two
	int 21h
	sub al,30h		;convert to number
	mov bl,0ah		;mov 10 to bl
	mul bl			;multiply 10 to al
	mov bl,al		;copy to bl

	mov ah,01h		;input 1s digit of number two
	int 21h
	sub al,30h		;convert to number
	add bl,al		;add to bl

	lea DI,b	 	;load address of b to index reg
	mov [DI],bl		;save bl to location of b (b = bl)
	
	;TO PRINT SUM RESULT
	lea dx,m3		;load effective address of add to dx
	mov ah,09h		;print result message
	int 21h

	;ACTUAL ADDITION
	mov ax,000h
	mov ax,a		;mov a to ax
	mov bx,b		;mov b to bx
	add ax,bx			;add bl to al
	mov bl,0ah		;save 10 to bx
	mov dx,0000h		;clear
	mov cx,0000h		;clear
	div bl			;div ax by bx
	
	mov cx,dx		;save dx(remainder) in bx because dx overwritten in printing

	add al,30h		;add 30 to convert to ascii
	mov dl,al		;mov to ax to print
	mov cl,ah
	mov ah,02h		;print quotient
	int 21h

	mov dl,cl		;mov to bx to print
	add dl,30h
	mov ah,02h		;print remainder
	int 21h

	;TO PRINT DIFFERENCE RESULT
	lea dx,m4		;load effective address of add to dx
	mov ah,09h		;print result message
	int 21h

	;ACTUAL SUBTRACTION
	mov ax,000h
	mov ax,a		;mov a to ax
	mov bx,b		;mov b to bx
	sub ax,bx		;sub bl to al
	mov bl,0ah		;save 10 to bx
	mov dx,0000h		;clear
	mov cx,0000h		;clear
	div bl			;div ax by bx


	add al,30h		;add 30 to convert to ascii
	mov dl,al		;mov to ax to print
	mov cl,ah
	mov ah,02h		;print quotient
	int 21h

	mov dl,cl		;mov to bx to print
	add dl,30h
	mov ah,02h		;print remainder
	int 21h

	;TO PRINT PRODUCT RESULT
	lea dx,m5		;load effective address of add to dx
	mov ah,09h		;print result message
	int 21h

	;ACTUAL MULTIPLICATION
	mov ax,000h
	mov ax,a		;mov a to ax
	mov bx,b		;mov b to bx
	mul bx			;mul bl to al
	mov bl,0ah		;save 10 to bx
	mov dx,0000h		;clear
	mov cx,0000h		;clear
	div bl			;div ax by bx


	add al,30h		;add 30 to convert to ascii
	mov dl,al		;mov to ax to print
	mov cl,ah
	mov ah,02h		;print quotient
	int 21h

	mov dl,cl		;mov to bx to print
	add dl,30h
	mov ah,02h		;print remainder
	int 21h

	;TO PRINT QUOTIENT RESULT
	lea dx,m6		;load effective address of add to dx
	mov ah,09h		;print result message
	int 21h

	;ACTUAL DIVISION
	mov ax,000h
	mov ax,a		;mov a to ax
	mov bx,b		;mov b to bx
	div bl			;div bl to al
	mov bl,0ah		;save 10 to bx
	mov dx,0000h		;clear
	mov cx,0000h		;clear
	div bl			;div ax by bx


	add al,30h		;add 30 to convert to ascii
	mov dl,al		;mov to ax to print
	mov cl,ah
	mov ah,02h		;print quotient
	int 21h

	mov dl,cl		;mov to bx to print
	add dl,30h
	mov ah,02h		;print remainder
	int 21h

	mov ah,4ch		;return control to the terminal
	int 21h
end start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment