Helpful Information
 
 
Category: Other Programming Languages
Have an Assembler question...

Hey all, i'm NOT a programmer at all, but in my masters program i'm in, I'm in a class which slightly covers it. The teacher has shown us basic commands in it, and now he has us doing a project that i'm completely lost on. I'm an IT guy, i'm a systems engineer so this stuff is like greek to me, and i'm hoping someone can help me, he didn't even tell me where to get a compiler!!! And he hasn't answered my emails :(
Sorry, I mean i'll show what I have, which you all will probably laugh at, but this is what he gave us..and i'll show you what were supposed to do...any help is SOO Appreciated...And i've had this problem in the past when I go to programming boards for help, i'm not doing this cause I don't want to do this work or can't understand it, but I was basically taught nothing concrete andhe didn't even assign our class a book. So please, don't think i'm just lazy.

Question at hand.
Euclid's algorithm, explained in class, twice, to calculate the highest common factor of two positive integers. You have my model
We then have to have it print our name on the screen and ID for grading purposes.

accept, after prompting, two multi-digit numbers, taken one digit at a time until the use presses {enter]. To get the value put the first digit into a safe register, then get the second, multiply the register by 10 and add in the new digit. Repeat until [enter] is pressed.

Then we must run it with documenting thing and accept the answer.

error check at the input stage -- make sure that only numbers are entered.


Now, heres what I have...



;Euclids Algorithm to find the highest common factor of 2 positive integers


.MODEL SMALL
.STACK 200h
.DATA
WELCOME DB 'Euclids Algorithm for HCF', 10d, 13d, 'for highest common factor', 10d, 13d, '$'

PROMPT1 DB 'Please type the first integer $'
PROMPT2 DB 10d,13d, 'Please type second integer$'
PROMPT3 DB 10d,13d, 'HCF is $'

.CODE
MOV BX, @DATA ; this two state move sets the
MOV DS,BX ; DS Register
MOV AH, 9d ; Bios for print a string
MOV DX,(offset) WELCOME ; code 9 expects the offset
INT 21h ; to be in DX - prints string at DS::DX
MOV AH,9d
MOV DX, (offset) PROMPT1
INT 21h ; after this , headings are complete
MOV AH, 1d ; get ready to read first integer
INT 21h ; invoke bios. Result left in AC. ASCII Code!
SUB AL, 30h ; sets AC to ACSII minus 30 hex
MOV CL,AL ; save first integer in CL
MOV AH,9d ; get ready to print second prompt
MOV DX, (offset) PROMPT2
INT21h ; after this read second integer's code
MOV AH,1d
INT21h
SUB AL,30h ; convert ASCII toreal value
REPEAT:
CMP AL,CL
JE PRINT_AND_EXIT
; if they are not equal
JG A_GREATER
; if here, b or CL is greater than AL
SUB CL,AL ; sets CL to CL-AL
JMP REPEAT
A_GREATER:
SUB AL,CL ; set AL = AL-CL
JMP REPEAT
PRINT_AND_EXIT:
MOV AH,0d
MOV DX,(offseT) PROMPT3
INT 21h
MOV DL,AL
ADD DL,30h
MOV AH,2d
INT 21h
; need to quit
MOV AH,4ch
INT 21h
END

This was basically code our professor gave us..and I read the sticky and i'm not asking for anyone to do my homework, but just help me cause i'm lost as heck!!!


Thanks in advance for any help...

EDIT: Also, If you can even recommend a 8086 compiler too, sinceI don't even have that to error check :-\

It sounds as if your instructor isn't doing his job, then... I would think that he would have at least provided the assembler you need. Assembly isn't something you can just handwave, really.

Unfortunately, without knowing which assembler the code is for, it won't be easy to help you; every different assembler has it's own slightly different syntax, and which a particular program is in isn't always obvious. The good news is, there are really only four or five likely options, all but one of which can be downloaded for free. MS Macro Assembler and Borland Turbo Assembler being at the top of the list; GNU Assembler uses a radically different syntax, and Netwide Assembler, while popular among open source developers, is uncommon for course work. Also, it shouldn't be difficult to convert the code to any other assembler if needed (though not as trivial as you might hope, sadly).

I would try MASM first, as that's the most commonly used for courses, and at least on a cursory glance it seems like the right syntax for it (though the code doesn't apply the PROC directive, which most - but not all - MASM code uses). Microsoft has made the executables to some older versions freely available; you can download v. 9.0 from here (http://www.masm32.com/masmdl.htm).

[EDIT: I've checked, and it doesn't assemble under either MASM or NASM as written (I'd been fairly certain it wasn't NASM code already, but I wanted to make sure, as Nasm has some compatibility modes which I don't know very well). It may be written for TASM, which is potentially problematic since that's not free. OTOH, there are at least two syntax errors in the code that would occur regardless of the assembler used, so it may simply be buggy.]

I will tell you that the code is written for MS-DOS rather than Windows, per se, though it should of course run under Windows. It uses the BIOS service call (Int 21h - the 'h' stands for hexadecimal, as opposed to 'd', for decimal) rather than the Windows equivalents, andin any case Windows assembly programs require a large amount of boilerplate code to handle the program set up.

I usually would recommend the book Assembly Language Step by Step (http://www.duntemann.com/assembly.htm) as a starting text, but since this is for a class and I don't know even which assembler you are using (ALSbS uses Nasm), I don't know if it would help you or confuse you.

Beyond that, what else do you need to know?

Links:
General Information
x86 Assembly Language FAQs (http://www.faqs.org/faqs/assembly-language/x86/)
Wikipedia: Assemblers (http://en.wikipedia.org/wiki/Assemblers)
WikiBooks: X86 Assembly (http://en.wikibooks.org/wiki/X86_Assembly)
Jeff Dunteman's Assembly Language Step by Step (http://www.duntemann.com/assembly.htm) (home page for a print book, but also has several useful links)
The Art of Assembly Language (http://artofassembly.com) and Write Great Code (http://www.writegreatcode.com/) (online books, primarily covering HLA)
Assembly Language Links (http://www.eeglossary.com/assembly.htm)
Webster Assembly Language Resources (http://webster.cs.ucr.edu/)
Paul Hsieh's x86 Assembly Language Page (http://www.azillionmonkeys.com/qed/asm.html)
The Graphics Programming Black Book (http://www.gamedev.net/reference/articles/article1698.asp) (extensively discusses assembly language in general, in addition to the specific topic; downloadable in PDF format)
Free Assemblers and Linkers (http://www.thefreecountry.com/compilers/assemblers.shtml)
Assembly Language Links (http://www.jorgon.freeserve.co.uk/links.htm)
Still more links (http://www.eg3.com/WebID/software/assembly/blank/resource/a-z.htm)
Assembler for PCs: A Tutorial (http://home.snafu.de/nkomin/html/assembe.htm)

MASM
MSDN: Microsoft Macro Assembler Technical Reference (http://msdn2.microsoft.com/en-us/library/afzk3475.aspx)
Wikipedia: Microsoft Macro Assembler (http://en.wikipedia.org/wiki/MASM)
The MASM32 Tool and Code Project (http://www.movsd.com/)
MASM32.com (http://www.masm32.com/)
MASM Forum (http://www.masmforum.com/simple/index.php)
MASM Doc Reference Page (http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/)

TASM
Turbo Assembler 5.0 home page (http://info.borland.com/borlandcpp/cppcomp/tasmfact.html)
Wikipedia: Turbo Assembler (http://en.wikipedia.org/wiki/Turbo_Assembler)

NASM
Netwide Assembler Home page (http://sourceforge.net/projects/nasm)
Wikipedia: Netwide Assembler (http://en.wikipedia.org/wiki/NASM)
NasmEdit and NASM-IDE (http://uk.geocities.com/rob_anderton/) (editors for Nasm)

gas
GNU binutils (http://www.gnu.org/software/binutils/) (includes GAS)
Cygwin (http://www.cygwin.com/) (a system for running GNU tools under Windows)
MingW (http://www.mingw.org/) (another system for running GNU tools under Windows)
Wikipedia: GNU Assembler (http://en.wikipedia.org/wiki/GNU_Assembler)

Other Assemblers and Tools
RadASM IDE (http://www.radasm.com/) (an editor that supports several different assemblers)
Wikipedia: RadASM (http://en.wikipedia.org/wiki/RadASM)

HLA (http://webster.cs.ucr.edu/AsmTools/HLA/)
Wikipedia: HLA (http://en.wikipedia.org/wiki/High_Level_Assembly)

FASM (http://flatassembler.net/)
Wikipedia: Flat Assembler (http://en.wikipedia.org/wiki/FASM)

RosASM (http://betov.free.fr/RosAsm.html)

I found out that its TASM and this is what the code should have looked like to start...



.model small
.stack 200h
.data
welcome db 'Running Euclids algorithm to find the HCF', 0Ah, 0Dh, 'of two small positive integers.', 0Ah, 0Dh, '$'
prompt2 db 0Ah, 0Dh, 'Please type the first integer $'
prompt3 db 0Ah, 0Dh, 'Please type the second integer $'
prompt4 db 0Ah, 0Dh, 'The HCF is $'
goodbye db 0AH, 0Dh, 'quitting now$'
.code
;first, set the pointer to the start of the data segment to provide access to the strings
mov dx, @data
mov ds, dx

;print opening prompt
mov dx, offset welcome ;set the offset to the 'welcome' string
mov ah, 9d ;BIOS code for print a dollar terminated string
int 21h ;invoke BIOS -- prints the string at DS::DX, always

;begin algorithm by reading in two 1-digit integers
;print the prompt first, using BIOS code '9d'
mov dx, offset prompt2
mov ah, 9d
int 21h

;now get the keystroke
mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
;save the keystroke in register 'cl'
;after we get the integer value from the ASCII code
;by subtracting hex 30 from the ASCII code value
sub al, 30h
mov cl,al

;now read the second integer as above
;first print the prompt using BIOS code '1d'
mov dx, offset prompt3 ;set the offset for DS::DX
mov ah, 9d ;set BIOS code
int 21h ;invoke BIOS

mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
sub ah, 30h
; we now have two integers, actual values not ASCII codes, one in 'cl' and the other in 'al'


repeat:
cmp cl, al ;compare the two integers
jz print_and_exit ;print the result and return control to operating system
;if here, the two numbers were **NOT** the same
;note that the flags are still set from the compare statement, but it's probably wise to do another
;one in case we change or edit the file.
cmp cl, al
jg greater ;test to see if cl > al, and if it is, jump to label 'greater'
;OK, so if we get to here, cl **must** be less than (<) al
sub al, cl ;diminish 'al' by 'cl'
jmp repeat ;go round the loop until the variables become equal

greater:
;if we get here, it was shown that cl > al, so diminish 'cl'
sub cl, al
jmp repeat ;go round the loop until the variables become equal


print_and_exit:
;there's only one way to get here and that's when the integers are equal
;so print and then we're done.

;first the prompt
mov dx, offset prompt4
mov ah, 9d ;BIOS code for print a string
int 21h

;last, the actual result
mov ah, 2d ;BIOS code for print to screen
;character's ASCII code must be in 'dl' for this to work
mov dl, cl ;starting to print 'cl's value
add dl, 30h ;convert value to ASCII code
int 21h

mov dx, offset goodbye
mov ah, 9d
int 21h

mov ah, 4ch ;BIOS code for exit/quit
int 21h

end

Ok, sorry to keep being a pain, but I seem to be doing OK with this program, but can someone tell me why in this program when it asks for the second value for the Highest common factor it just keeps asking over and over again? Thanks in advance.




.model small
.stack 200h
.data
welcome db 'Running Euclids algorithm to find the HCF', 0Ah, 0Dh, 'of two small positive integers.', 0Ah, 0Dh, '$'
nameoutput db 'Rose, Matthew 0471844',0Ah, 0Dh, '$'
prompt2 db 0Ah, 0Dh, 'Please type the first integer $'
prompt3 db 0Ah, 0Dh, 'Please type the second integer $'
prompt4 db 0Ah, 0Dh, 'The HCF is $'
goodbye db 0AH, 0Dh, 'quitting now$'
.code
;first, set the pointer to the start of the data segment to provide access to the strings
mov dx, @data
mov ds, dx
;print name and ID
mov dx, offset nameoutput ;sets the offset to the nameoutput string
mov ah,9d ;code to print a dollar terminated string
int 21h ;prints the string at DS::DX
;print opening prompt
mov dx, offset welcome ;set the offset to the 'welcome' string
mov ah, 9d ;BIOS code for print a dollar terminated string
int 21h ;invoke BIOS -- prints the string at DS::DX, always

;begin algorithm by reading in integers
;print the prompt first, using BIOS code '9d'
mov dx, offset prompt2
mov ah, 9d
int 21h
MOV AL, 0d
MUL CX
MUL BX
enter_loop1:
;now get the keystroke
mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
cmp AL,13d
JZ next_integer
;save the keystroke in register 'cl'
;after we get the integer value from the ASCII code
;by subtracting hex 30 from the ASCII code value
sub al, 30h
add cl,al
JMP enter_loop1


next_integer:
;now read the second integer as above
;first print the prompt using BIOS code '1d'
mov dx, offset prompt3 ;set the offset for DS::DX
mov ah, 9d ;set BIOS code
int 21h ;invoke BIOS

mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
cmp al,13d
JZ repeat
sub ah, 30h
add bl,al
JMP next_integer
; we now have two integers, actual values not ASCII codes, one in 'cl' and the other in 'al'


repeat:
cmp cl, bl ;compare the two integers
jz print_and_exit ;print the result and return control to operating system
;if here, the two numbers were **NOT** the same
;note that the flags are still set from the compare statement, but it's probably wise to do another
;one in case we change or edit the file.
cmp cl, bl
jg greater ;test to see if cl > bl, and if it is, jump to label 'greater'
;OK, so if we get to here, cl **must** be less than (<) bl
sub bl, cl ;diminish 'bl' by 'cl'
jmp repeat ;go round the loop until the variables become equal

greater:
;if we get here, it was shown that cl > bl, so diminish 'cl'
sub cl, bl
jmp repeat ;go round the loop until the variables become equal


print_and_exit:
;there's only one way to get here and that's when the integers are equal
;so print and then we're done.

;first the prompt
mov dx, offset prompt4
mov ah, 9d ;BIOS code for print a string
int 21h

;last, the actual result
mov ah, 2d ;BIOS code for print to screen
;character's ASCII code must be in 'dl' for this to work
mov dl, cl ;starting to print 'cl's value
add dl, 30h ;convert value to ASCII code
int 21h

mov dx, offset goodbye
mov ah, 9d
int 21h

mov ah, 4ch ;BIOS code for exit/quit
int 21h

end

Without giving a direct answer, I think you'll find that problem is in the loop that reads in the second number, and specifically, with where it loops back to:



next_integer:
;now read the second integer as above
;first print the prompt using BIOS code '1d'
mov dx, offset prompt3 ;set the offset for DS::DX
mov ah, 9d ;set BIOS code
int 21h ;invoke BIOS

mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
cmp al,13d
JZ repeat
sub ah, 30h
add bl,al
JMP next_integer


The red section is where it prompts for the second number; the blue is where it reads it, one digit at a time, checking to see if it has reached the end of the number after each one. Furthermore, the blank line in the middle there is a hint as well. Compare it to the similar loop above it that reads the first number:


;print the prompt first, using BIOS code '9d'
mov dx, offset prompt2
mov ah, 9d
int 21h
MOV AL, 0d
MUL CX
MUL BX
enter_loop1:
;now get the keystroke
mov ah, 1d ;BIOS code for read a keystroke
int 21h ;invoke BIOS, ASCII code will be in register 'al' after this command
cmp AL,13d
JZ next_integer
;save the keystroke in register 'cl'
;after we get the integer value from the ASCII code
;by subtracting hex 30 from the ASCII code value
sub al, 30h
add cl,al
JMP enter_loop1


I don't want to answer it for you, but the hints should be pointed enough.

Its ironic, you answered that now, I had finally noticed that. I fixed that, now I realize its not doing ANY kind of HCF stuff...arg...

Sorry for taking so long to get back... anyway, I was having trouble following the code, and didn't have a copy of TASM on hand in any case, so I re-wrote the code to work in NASM instead, replacing most of the magic numbers with EQUates, along the way (and fixing some of the comments - the interrupt calls were to DOS, not the BIOS):



[org 0x0100]

dos equ 0x021 ; interrupt for DOS function selector
getchar equ 0x02 ; DOS routine code "read character"
putchar equ 0x02 ; DOS routine code "print character"
puts equ 0x09 ; DOS routine code "print string"
quit equ 0x4c ; DOS routine code "return to DOS"

CR equ 0x0A ; ASCII carriage return
LF equ 0x0D ; ASCII line feed
off_zed equ 0x30 ; ASCII zero


[section code]

;print name and ID
mov dx, nameoutput ;sets the offset to the nameoutput string
mov ah, puts ;code to print a dollar terminated string
int dos ;prints the string at DS::DX

;print opening prompt
mov dx, welcome ;set the offset to the 'welcome' string
mov ah, puts ;DOS code for print a dollar terminated string
int dos ;invoke DOS -- prints the string at DS::DX, always

;begin algorithm by reading in integers
;print the prompt first, using DOS code '9d'
mov dx, prompt2
mov ah, puts
int dos
mov al, 0
mul cx
mul bx

enter_loop1:
;now get the keystroke
mov ah, getchar ;DOS code for read a keystroke
int dos ;invoke DOS, ASCII code will be in register 'al' after this command
cmp al, LF
jz next_integer

;save the keystroke in register 'cl'
;after we get the integer value from the ASCII code
;by subtracting hex 30 from the ASCII code value
sub al, off_zed
add cl, al
jmp enter_loop1


next_integer:
;now read the second integer as above
;first print the prompt using DOS code '1d'
mov dx, prompt3 ;set the offset for DS::DX
mov ah, puts ;set dos code
int dos ;invoke dos

enter_loop2:
mov ah, getchar ;dos code for read a keystroke
int dos ;invoke dos, ASCII code will be in register 'al' after this command
cmp al, LF
jz repeat
sub ah, off_zed
add bl, al
jmp enter_loop2
; we now have two integers, actual values not ASCII codes, one in 'cl' and the other in 'al'


repeat:
cmp cl, bl ;compare the two integers
jz print_and_exit ;print the result and return control to operating system
;if here, the two numbers were **NOT** the same

;note that the flags are still set from the compare statement, but it's probably wise to do another
;one in case we change or edit the file.
cmp cl, bl
jg greater ;test to see if cl > bl, and if it is, jump to label 'greater'

;OK, so if we get to here, cl **must** be less than (<) bl
sub bl, cl ;diminish 'bl' by 'cl'
jmp repeat ;go round the loop until the variables become equal

greater:
;if we get here, it was shown that cl > bl, so diminish 'cl'
sub cl, bl
jmp repeat ;go round the loop until the variables become equal


print_and_exit:
;there's only one way to get here and that's when the integers are equal
;so print and then we're done.

;first the prompt
mov dx, prompt4
mov ah, puts ;dos code for print a string
int dos

;last, the actual result
mov ah, putchar ;dos code for print to screen

;character's ASCII code must be in 'dl' for this to work
mov dl, cl ;starting to print 'cl's value
add dl, off_zed ;convert value to ASCII code
int dos

mov dx, goodbye
mov ah, puts
int dos

mov ah, quit ;dos code for exit/quit
int dos

[section data]
welcome db "Running Euclids algorithm to find the HCF", CR, LF
db "of two small positive integers.", CR, LF, "$"
nameoutput db "Rose, Matthew 0471844", CR, LF, "$"
prompt2 db CR, LF, "Please type the first integer $"
prompt3 db CR, LF, "Please type the second integer $"
prompt4 db CR, LF, "The HCF is $"
goodbye db CR, LF, "quitting now$"

One thing I noticed is that the code for both reading and printing the numbers is wrong - if you are reading more than one digit, you aren't adjusting the digits for place value, with the practical upshot that you are adding the digits together rather than reading them as a single number. For example, if you entered '1234', the number that would actually get stored in CL is 1 + 2 + 3 + 4 = 10. Similarly, you are printing the number as a single ASCII character value, not as a number.

Niether of these problems is necessarily the cause of the freezing, though. I will see if I can take a closer look at the code RSN. In the meanwhile, you might want to look at the Interrrupt List (http://www.ctyme.com/rbrown.htm) to better understand the behavior of the DOS functions in question (especially INT 0x21, ah=0x01 (http://www.ctyme.com/intr/rb-2552.htm), INT 0x21, ah=0x02 (http://www.ctyme.com/intr/rb-2554.htm), and INT 0x21, ah=0x09 (http://www.ctyme.com/intr/rb-2562.htm)) ; some of the have side effects that can change the values in the BX and AX registers. You might want to look into INT 0x21, ah=0x0A (http://www.ctyme.com/intr/rb-2563.htm) as well, for a way of getting buffered input rather than reading each character directly.










privacy (GDPR)