# learner needs assembler help - adding numbers

hello,

i have just started to learn how to do assembler and i am really really stuck. i need to write a program in 80x86 that will get five integer values and then determine the largest, smallest, and the average. i know how to do this in C programming but i can't work out how to do it in assembler.

i am honestly stuck with no ideas, i have tried searching the net for sample programs but no real luck. so i really need some help

alex

• Take a look at the steps which you need to do the task:

1. reading numbers from the keyboard.

this could be done with the standart-dos-input,i think its func 0x0a from int21h.get a copy of ralph browns intlist,to get a detailed description of it.

2. convert the string to a binary number.this could be easily done with a loop in which you take each char from left to right and sub the char '0' of it and then multiply with ten.

3. now make your calculations.this shouldnt be that difficult,searching could be done in a loop,with little asm-knwledge you could convert your c/c++ code to asm.

4. convert your binary results back to a string and output it with func09 of int21h.
• i just want to say thanks for replying..

i have an idea of the steps to complete the program but i have no idea on how to type the assembler language!! you don't happen to have any programs that are similar to this so i can use them as a model to make the program??

thanks again
alex
: Take a look at the steps which you need to do the task:
:
: 1. reading numbers from the keyboard.
:
: this could be done with the standart-dos-input,i think its func 0x0a from int21h.get a copy of ralph browns intlist,to get a detailed description of it.
:
: 2. convert the string to a binary number.this could be easily done with a loop in which you take each char from left to right and sub the char '0' of it and then multiply with ten.
:
: 3. now make your calculations.this shouldnt be that difficult,searching could be done in a loop,with little asm-knwledge you could convert your c/c++ code to asm.
:
: 4. convert your binary results back to a string and output it with func09 of int21h.
:

• : i just want to say thanks for replying..
:
: i have an idea of the steps to complete the program but i have no idea on how to type the assembler language!! you don't happen to have any programs that are similar to this so i can use them as a model to make the program??

[blue]I have some code samples on my site: www.codexxi.com -> Components[/blue]

:
: thanks again
: alex
: : Take a look at the steps which you need to do the task:
: :
: : 1. reading numbers from the keyboard.
: :
: : this could be done with the standart-dos-input,i think its func 0x0a from int21h.get a copy of ralph browns intlist,to get a detailed description of it.
: :
: : 2. convert the string to a binary number.this could be easily done with a loop in which you take each char from left to right and sub the char '0' of it and then multiply with ten.
: :
: : 3. now make your calculations.this shouldnt be that difficult,searching could be done in a loop,with little asm-knwledge you could convert your c/c++ code to asm.
: :
: : 4. convert your binary results back to a string and output it with func09 of int21h.
: :
:
:

• :I have no idea on how to type the assembler language!!
:you don't happen to have any programs that are similar to this
:so i can use them as a model to make the program??

Have you chosen an assembler yet?
Nasm is free and very good.
I have a nasm environment ready to goto work in DOS
you can have. It's got blank template.asm's
and batch files to make and assemble them.
Include files & macro's to do number conversions to/from ASCII
in memory, to screen, or direct video memory. At
http://bitdog.home.att.net/files/nasmenv.zip
It has a fairly standard & simple syntax.
Some code that finds the largest, smallest, & average might look like this untested code. (just to give an idea of what to do)

MOV BX,0x00FF ;store the biggest in BH & smallest in BL
MOV SI,address of string, or get key at TOP:.
XOR DX,DX ;total value storage, number values can't exceed 65,535
XOR CX,CX ;number counter
XOR AH,AH ;so AX can add AL to DX below
TOP:
LODSB ;get value in AL or use kbd input?
; CMP AL,27 ; JZ BYE for ESC keypress, AH=8 INT 21h get key
OR AL,AL ;nul ending string? or use length counter? or key press ESC?
JZ DOMATH ;end the input cmp part & do the math below
CMP AL,BH
JB NOTBIG
MOV BH,AL ;if AL is bigger, save it
NOTBIG:
CMP AL,BL
JAE NOTSMALLER
MOV BL,AL ;keep the smaller of the two
NOTSMALLER:
INC CX ;counter
JMP SHORT TOP ;yer loops end
DOMATH:
;Print, the biggest number was (value of BH here)
;Print, the smallest number was (value of BL here)
JCXZ NOINPUTERROR ;avoid a divide by zero error
MOV AX,DX ;move total value to AX
XOR DX,DX ;this line might not be needed
DIV CL ;divide AX=total, by CL=count, gives AX=average
;Print, the average was (value of AX here)
NOINPUTERROR: ;there was no input

;That's probably fraut with errors, but it's a start
and maybe some one will throw in their 2 cents
and corrections, er working code.

Bitdog

• [code]

TITLE Simple Sort

; AUTHOR Yuri Margolin
; http://mysoft.s5.com/
;
; DATE 23/01/01
; VERSION 1.02
; FILE sort.asm

; THIS PROGRAM INPUTS 3 NUMBERS AND
; SORTS THEM FROM LARGEST TO SMALLEST

#MAKE_EXE#

; contains some common
; procedures and macros,
; (definition is done in the bottom
; of this file):
INCLUDE 'emu8086.inc'

DSEG SEGMENT 'DATA'
CR EQU 0Dh
LF EQU 0Ah
DOLLAR EQU '\$'
NEW_LINE DB LF, CR, DOLLAR
MSG1 DB 'ENTER first value (-32768..32767)!'
DB LF, CR, DOLLAR
MSG2 DB LF, CR, 'ENTER second value (-32768..32767)!'
DB LF, CR, DOLLAR
MSG3 DB LF, CR, 'ENTER third value (-32768..32767)!'
DB LF, CR, DOLLAR
MSG4 DB CR, LF, CR, LF, 'After sorting:', DOLLAR
MSG5 DB CR, LF, 'num1 = ', DOLLAR
MSG6 DB CR, LF, 'num2 = ', DOLLAR
MSG7 DB CR, LF, 'num3 = ', DOLLAR
num1 DW ?
num2 DW ?
num3 DW ?
DSEG ENDS

SSEG SEGMENT STACK 'STACK'
DW 100h DUP(?)
SSEG ENDS

CSEG SEGMENT 'CODE'

;****************************************

START PROC FAR

PUSH DS
MOV AX, 0
PUSH AX
; Set segment registers:
MOV AX, DSEG
MOV DS, AX
MOV ES, AX

; Clear the screen:
CALL clear_screen

; Position the cursor at row=3 and column=0
GOTOXY 0, 3

LEA DX, MSG1
CALL PUTS ; Display the message.
CALL SCAN_NUM ; Input the number into CX.
MOV num1, CX

LEA DX, MSG2
CALL PUTS ; Display the message.
CALL SCAN_NUM ; Input the number into CX.
MOV num2, CX

LEA DX, MSG3
CALL PUTS ; Display the message.
CALL SCAN_NUM ; Input the number into CX.
MOV num3, CX

; Sorting:
MOV BX, num1
MOV CX, num2
CALL SORT ; exchange if BX<CX
MOV num1, BX
MOV num2, CX

MOV BX, num2
MOV CX, num3
CALL SORT ; exchange if BX<CX
MOV num2, BX
MOV num3, CX

MOV BX, num1
MOV CX, num2
CALL SORT ; exchange if BX<CX
MOV num1, BX
MOV num2, CX

; Print the result:
LEA DX, MSG4
CALL PUTS

LEA DX, MSG5
CALL PUTS
MOV AX, num1
CALL PRINT_NUM ; print AX.

LEA DX, MSG6
CALL PUTS
MOV AX, num2
CALL PRINT_NUM ; print AX.

LEA DX, MSG7
CALL PUTS
MOV AX, num3
CALL PRINT_NUM ; print AX.

LEA DX, NEW_LINE
CALL PUTS

RET
START ENDP

;***********************************

; uses DOS function to print:
PUTS PROC NEAR
PUSH AX
MOV AH, 09h
INT 21h
POP AX
RET
PUTS ENDP

;************************************

; if BX < CX then exchanges them
; (works with signed numbers)
SORT PROC NEAR
CMP BX, CX
JGE compared
XCHG BX, CX
compared:
RET
SORT ENDP

;************************************

; Definition of some common procedures
; from emu8086.inc:

DEFINE_CLEAR_SCREEN
DEFINE_PRINT_STRING

DEFINE_SCAN_NUM

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

;*************************************

CSEG ENDS
END START

[/code]

• thank you all for your help, it has been more than help full

: [code]
:
: TITLE Simple Sort
:
: ; AUTHOR Yuri Margolin
: ; http://mysoft.s5.com/
: ;
: ; DATE 23/01/01
: ; VERSION 1.02
: ; FILE sort.asm
:
: ; THIS PROGRAM INPUTS 3 NUMBERS AND
: ; SORTS THEM FROM LARGEST TO SMALLEST
:
: #MAKE_EXE#
:
: ; contains some common
: ; procedures and macros,
: ; (definition is done in the bottom
: ; of this file):
: INCLUDE 'emu8086.inc'
:
:
: DSEG SEGMENT 'DATA'
: CR EQU 0Dh
: LF EQU 0Ah
: DOLLAR EQU '\$'
: NEW_LINE DB LF, CR, DOLLAR
: MSG1 DB 'ENTER first value (-32768..32767)!'
: DB LF, CR, DOLLAR
: MSG2 DB LF, CR, 'ENTER second value (-32768..32767)!'
: DB LF, CR, DOLLAR
: MSG3 DB LF, CR, 'ENTER third value (-32768..32767)!'
: DB LF, CR, DOLLAR
: MSG4 DB CR, LF, CR, LF, 'After sorting:', DOLLAR
: MSG5 DB CR, LF, 'num1 = ', DOLLAR
: MSG6 DB CR, LF, 'num2 = ', DOLLAR
: MSG7 DB CR, LF, 'num3 = ', DOLLAR
: num1 DW ?
: num2 DW ?
: num3 DW ?
: DSEG ENDS
:
: SSEG SEGMENT STACK 'STACK'
: DW 100h DUP(?)
: SSEG ENDS
:
: CSEG SEGMENT 'CODE'
:
: ;****************************************
:
: START PROC FAR
:
: PUSH DS
: MOV AX, 0
: PUSH AX
: ; Set segment registers:
: MOV AX, DSEG
: MOV DS, AX
: MOV ES, AX
:
: ; Clear the screen:
: CALL clear_screen
:
: ; Position the cursor at row=3 and column=0
: GOTOXY 0, 3
:
:
: ; Ask for first number:
: LEA DX, MSG1
: CALL PUTS ; Display the message.
: CALL SCAN_NUM ; Input the number into CX.
: MOV num1, CX
:
: ; Ask for second number:
: LEA DX, MSG2
: CALL PUTS ; Display the message.
: CALL SCAN_NUM ; Input the number into CX.
: MOV num2, CX
:
: ; Ask for third number:
: LEA DX, MSG3
: CALL PUTS ; Display the message.
: CALL SCAN_NUM ; Input the number into CX.
: MOV num3, CX
:
: ; Sorting:
: MOV BX, num1
: MOV CX, num2
: CALL SORT ; exchange if BX<CX
: MOV num1, BX
: MOV num2, CX
:
: MOV BX, num2
: MOV CX, num3
: CALL SORT ; exchange if BX<CX
: MOV num2, BX
: MOV num3, CX
:
: MOV BX, num1
: MOV CX, num2
: CALL SORT ; exchange if BX<CX
: MOV num1, BX
: MOV num2, CX
:
: ; Print the result:
: LEA DX, MSG4
: CALL PUTS
:
: LEA DX, MSG5
: CALL PUTS
: MOV AX, num1
: CALL PRINT_NUM ; print AX.
:
: LEA DX, MSG6
: CALL PUTS
: MOV AX, num2
: CALL PRINT_NUM ; print AX.
:
: LEA DX, MSG7
: CALL PUTS
: MOV AX, num3
: CALL PRINT_NUM ; print AX.
:
: LEA DX, NEW_LINE
: CALL PUTS
:
: RET
: START ENDP
:
: ;***********************************
:
: ; Displays the message (DX-address),
: ; uses DOS function to print:
: PUTS PROC NEAR
: PUSH AX
: MOV AH, 09h
: INT 21h
: POP AX
: RET
: PUTS ENDP
:
: ;************************************
:
: ; if BX < CX then exchanges them
: ; (works with signed numbers)
: SORT PROC NEAR
: CMP BX, CX
: JGE compared
: XCHG BX, CX
: compared:
: RET
: SORT ENDP
:
: ;************************************
:
: ; Definition of some common procedures
: ; from emu8086.inc:
:
: DEFINE_CLEAR_SCREEN
: DEFINE_PRINT_STRING
:
: DEFINE_SCAN_NUM
:
: DEFINE_PRINT_NUM
: DEFINE_PRINT_NUM_UNS
:
: ;*************************************
:
: CSEG ENDS
: END START
:
: [/code]
:
: