My teacher sucks and I really want to learn ASM. I have a simple program due but I can't figure it out.

Here's the program...

Write a program to display a "?", read two decimal digits and display them and their sum on the next line, with an approiate message.

Sample execution:

?27
THE SUM OF 2 AND 7 IS 9


I'm a beginner at programming, and I've been trying to figure this out for a while. Plz help, THX
Posted on 2002-02-27 17:33:23 by newb
Not tested but here goes...



.data
value db "?27", 0

.code

...

xor eax, eax
xor edx, edx

lea esi, value
mov al, BYTE ptr [esi+1]
mov dl, BYTE ptr [esi+2]
add eax, edx

...



There you go, you can print directly ?27 and the sum of 2 and 7 is in eax. Just wondering, is this a 16bit program?
Posted on 2002-02-27 17:41:53 by stryker
Is this for a real-mode ASM course (16 bit DOS)?
Which assembler?

POST some of your code, then we can help you.
Posted on 2002-02-27 17:44:56 by bdjames
Here's what I got so far...

TITLE PGM1: SAMPLE PROGRAM
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 0DH, 0AH, 'THE SUM OF '
A DW ?
MSG2 DB 'AND '
B DW ?
MSG3 DB 'IS '
SUM DW ?,'$'
.CODE
MAIN PROC
;Initialize DS
MOV AX, @DATA
MOV DS, AX
;DISPLAY USER PROMPT
MOV AH,2
MOV DL,'?'
INT 21H
;INPUT FIRST NUMBER
MOV AH, 1
INT 21H
MOV A, AL
;INPUT SECOND NUMBER
MOV AH, 1
INT 21H
MOV B, AL
;Add the numbers
MOV AX, A
ADD AX, B
MOV SUM, AX
;DISPLAY ON NEXT LINE
LEA DX, MSG1
MOV AH, 9
INT 21H
MAIN ENDP
END MAIN

I know alot of this is probably wrong though....
Posted on 2002-02-27 17:55:52 by newb
This isn't optimized but should do the job for you! Works perfect with TASM:



.MODEL SMALL
.STACK 256

.DATA

value DB "?27", 0Dh, 0Ah, "$"
hd1 DB "The sum of $"
hd2 DB " and $"
hd3 DB " is $"

.CODE

Start:

mov ax, @data
mov ds, ax

mov dx, OFFSET value
mov ah, 09h
int 21h

mov dx, OFFSET hd1
mov ah, 09h
int 21h

mov si, OFFSET value

xor dx, dx
mov dl, BYTE PTR [si+1]
mov ah, 02h
int 21h

mov dx, OFFSET hd2
mov ah, 09h
int 21h

xor dx, dx
mov dl, BYTE PTR [si+2]
mov ah, 02h
int 21h

mov dx, OFFSET hd3
mov ah, 09h
int 21h

xor ax, ax
xor dx, dx
mov al, BYTE PTR [si+1]
sub al, 30h
mov dl, BYTE PTR [si+2]
sub dl, 30h
add dx, ax
add dl, 30h

xor ax, ax
mov ah, 02h
int 21h

mov ah, 4Ch
mov al, 0
int 21h

END Start


and yes, I still remember my 16 bit DOS days :grin: ha! ha! ha! :grin:

Happy ASMing!!!
Posted on 2002-02-27 18:20:07 by stryker
I made some corrections.
You should try to optimize.
Look at http://www.ctyme.com/intr/int.htm

;TITLE PGM4_8: DECIMAL DIGIT ADDITION PROGRAM

.MODEL SMALL
.8086
.STACK 100H

.DATA

MSG DB 13, 10, 'THE SUM OF '
NUM1 DB ?, ' AND '
NUM2 DB ?, ' IS '
SUM DB '$','$','$'

.CODE

MAIN proc

;INITIALIZE DS
MOV AX, @DATA
MOV DS, AX

;Print prompt
MOV AH,2
MOV DL,'?'
INT 21H

;INPUT FIRST NUMBER
MOV AH, 1 ;READ CHARACTER FUNCTION
INT 21H ;CHARACTER IN AL
MOV , AL ;SAVE IT IN NUM1
MOV BL, AL

;INPUT SECOND NUMBER
MOV AH, 1 ;READ CHARACTER FUNCTION
INT 21H ;CHARACTER IN AL
MOV , AL
ADD BL, AL

;Print
CMP BL, 9 + '0' + '0' ; Why?
JA greaterThanNine
SUB BL, '0'
MOV , BL
MOV DX, offset MSG ;GET FIRST MESSAGE
MOV AH, 9 ;DISPLAY STRING FUNCTION
INT 21H ;DISPLAY MESSAGE AND SUM
MOV AH, 4CH
INT 21H ;DOS EXIT

greaterThanNine:
MOV , '1'
SUB BL, 10 + '0'
MOV , BL
MOV DX, offset MSG ;GET FIRST MESSAGE
MOV AH, 9 ;DISPLAY STRING FUNCTION
INT 21H ;DISPLAY MESSAGE AND SUM
MOV AH, 4CH
INT 21H ;DOS EXIT

MAIN endp
END MAIN
Posted on 2002-02-27 19:24:05 by bdjames
Actually, haven't look at the code really hard but as long as your version is working :alright: oops It has to have user input, geee no wonder my code is different :)
Posted on 2002-02-27 19:28:57 by stryker
Thanks alot!

:)
Posted on 2002-02-28 17:54:50 by newb