Skip to content

Instantly share code, notes, and snippets.

@rudrasohan
Created November 18, 2019 18:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudrasohan/3d2f780093295cd88554d1e83337ede6 to your computer and use it in GitHub Desktop.
Save rudrasohan/3d2f780093295cd88554d1e83337ede6 to your computer and use it in GitHub Desktop.
Common assembly quick-notes for MASM

Strings

DF: Direction flags to determine in which direction the operations will take place. (SI, DI responsible)

CLD: DF=0
STD: DF=1

MOVSB: Copies one character off a string into another (similarly MOVSW)

REP: Repeats in CX

MOVSB: Moves contents of the byte addressed by DS:SI to ES:DI

STOSB: copies char at AL to DI LOC

LODSB: Loads char pointed by SI into AL

SCASB: (AL - ES:DI) sets ZF=1 if the val is zero (similarly SCASW)

REPNE: Repeat the work until ZF=1 returns zero (opposite REPNZ) 

CMPSB: compares two strings char by char sets ZF

REPE: comparison while equal while CX can also set less and greater than flag by SF

REPZ: comparison while zero while CX

Macro

No transfer of control
	name MAXRO x,y,z,...
		some shit
	endm 
to avoid jump conflicts
	GET_BIG MACRO W1, W2
		LOCAL EXIT
		Mov AX, W1
		Cmp AX, W2
		JG EXIT
		Mov AX, W2
	EXIT:
		ENDM
Recursive macros are possible

REPT: Repeats statements
	REPT 5 
	DW 0
	ENDM
	FACT MACRO N
		M = 1
		FAC = 1
		REPT N
			M = M + 1
			FACT = M*FACT
		ENDM
	ENDM
The IRP (indefinite repeat)
IRP d <a1,a2,a3,...>
	SACE_REGS MACRO REGS
		IRP D, <REGS>
			PUSH D
		ENDM
	ENDM
Conditionals:
- IF const exp is nonzero
- IFE '' '' is zero
- IFB arg is missing
- IFNB '' '' not missing
- IFDEF  symobl is not defined
- IDNDEF  ''   ''  is defined

.ERR for throwing errors

Procs & Segments

Procs

NEAR/FAR : Procedure is in the same seg

EXTERN : Decleration in another module

PUBLIC : proc must be declared public to be used in another module 

Segment

Align Type:	
-PARA: begings at next available para LSD is 0
-BYTE: begins at next available byte
-WORD: '' '' at next available word
-PAGE: begins at next available page

Combine Type:
	-PUBLIC: seg with same name are concatenated
	-COMMON: seg with same name begin at same plce in mem
	-STACK: same as public, but offset address is SS
	-AT paragraph: seg should begin at seperate para
	-Default: seperate segs

	ASSUME: asm needs to be told which seg is what

Recursion

	FACT proc
	PUSH BP
	Mov BP, SP
	Cmp word ptr [BP+4], 1
	JG E1
	Mov AX, 1
	Jmp R1
E1:
	Mov CX, [BP+4]
	Dec CX
	Push CX
	Call FACT
	MUL word ptr [BP+4]
R1:
	POP BP
	RET 2 ;(clears out 2 additional bytes)
	FACT ENDP

Adv Arithmetic

Adc: add with carry 
SBB: sub with carry

Binary coded decimal

packed BCD: 1 byte contains 2BCD
unpacked BCD: '' '' '' 1BCD

59 = 3Bh 00111011
   =     00000011 00001011 

AAA - Add
AAS - Subtract
AAM - mul
AAD - div

8087

Main diff is floating point options.
Integer and Real

10 bit packed bcd available

Register 8-80 bits available

Load:
	FLD - Real NO
	FILD - Integer
	FBLD - Packed BCD

Store:
	FST - real
	FSTP - real and pop
	** similar convention for others

INTEL Instruction Format

1 : Prefix-Byte: 0-4 bytes of special prefix values that affect the operation of the instruction.
2: Opcode: 1-2 byte
3: Mod R/M: 1-byte that specifies the addressing mode and instruction operand size.
4: Scaled Indexed Byte: If the instruction is using scaled index addressing mode.
5: Displcement: 1-byte or 1-word that specifies a memory address displcement for the instruction
6: Immediate: 1-byte or 1-word constant value if the instruction has an immediate operand. 

Assembly Process

-The first pass assembler generates macode for all except specific instruction.
-In the second pass the remaining machine codes are generated.

Encoding ADD CL, AL

000000 0 0 11 000 001

Operation code

OP-Codes have the variable included.

Operation code(op-code) | Operand

LD(Load)A(register), 01h =A=> 00111110 00000001 (3E 01)

INC A =A=> 00111100 (3C)

Assembly process

Pass1:

- Define symbols and literals and remember them in symbol table and literal table respectively.
- Keep track of location counter
- Process pseudo-operations

Pass2:

- Generate object code by converting symbolic op-code into respective numeric op-code
- Generate data for literals and look for values of symbols
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment