Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Learning MASM - nested IF THEN ELSE

76 views
Skip to first unread message

Geoffrey Brophy

unread,
May 3, 2014, 1:49:01 PM5/3/14
to
To the group:

I am new to assembly language. I am currently teaching myself MASM.
I would like to post some of my programs that I create while I am learning.
Perhaps this will benefit others who are starting MASM as well.

If anyone does not want me to post this here, please let me know.
If anyone has any correction or comment for this post, please remember that I am just

starting to learn MASM and I am not familiar with other assembly languages.

Thank you.
yesiamanerd

*** Here is a sample of a program using nested IF-THEN-ELSE statments:

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\msvcrt.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\msvcrt.lib

printf PROTO C :DWORD, printlist:VARARG
scanf PROTO C :DWORD, inputlist:VARARG

.data
msg1fmt byte 0Ah,"%s%d",0
msg2fmt byte 0Ah,"%s",0Ah,0
msg1 byte "a1: ",0
msg2 byte "b1: ",0
msg3 byte "c1: ",0
msg4 byte "d1: ",0
msg5 byte "Variable values, before program is run:",0
msg6 byte "Variable values, after program is run: ",0
msg7 byte "*** IF - THEN - ELSE TEST ***"
a1 sdword 3
b1 sdword 4
c1 sdword 5
d1 sdword 6

.code
main:
INVOKE printf, ADDR msg2fmt, ADDR msg5
INVOKE printf, ADDR msg1fmt, ADDR msg1, a1
INVOKE printf, ADDR msg1fmt, ADDR msg2, b1
INVOKE printf, ADDR msg1fmt, ADDR msg3, c1
INVOKE printf, ADDR msg1fmt, ADDR msg4, d1

if01: mov eax,a1
cmp eax,b1
jle else01
then01: dec a1
jmp endif01
else01: nop
if02: mov eax,b1
cmp eax,c1
jl else02
then02: sub eax,2
mov b1,eax
jmp endif02
else02: nop
if03: mov eax,c1
cmp eax,d1
jle else03
then03: add eax,d1
mov c1,eax
jmp endif03
else03: nop
mov eax,d1
mov ebx,2
cdq
div ebx
mov d1,eax
jmp endif03
endif03: nop
endif02: nop
endif01: nop

INVOKE printf, ADDR msg2fmt, ADDR msg6
INVOKE printf, ADDR msg1fmt, ADDR msg1, a1
INVOKE printf, ADDR msg1fmt, ADDR msg2, b1
INVOKE printf, ADDR msg1fmt, ADDR msg3, c1
INVOKE printf, ADDR msg1fmt, ADDR msg4, d1

ret
END main




*** Here is a the compile instructions that I used and the output:

C:\dev\CH4>\masm32\bin\ml /c /Zd /coff 2.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997. All rights reserved.

Assembling: 2.asm

***********
ASCII build
***********


C:\dev\CH4>\masm32\bin\Link /SUBSYSTEM:CONSOLE 2.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


C:\dev\CH4>2

Variable values, before program is run:

a1: 3
b1: 4
c1: 5
d1: 6
Variable values, after program is run:

a1: 3
b1: 4
c1: 5
d1: 3


*** The original C code was:
if ( a > b )
a = a - 1;
else
if ( b >= c )
b = b - 2;
else
if ( c > d)
c = c + d;
else
d = d / 2;

0 new messages