Question:
What is wrong with my assembly code ?
2012-12-23 13:56:21 UTC
what is wrong with my assembly code ??????

org 100h

mov cx,5 ; number of byte to input
mov dl,0 ;intializing count
first : mov ah,1 ;function for inputing values
int 21h
aaa ;adjusting ASCII
cmp cx,5
jz one ;save 1st value
cmp cx,4
jz two ;save 2nd value
cmp cx,3
jz three ;save 3rd value
cmp cx,2 ;save 4th value
jz four
cmp cx,1
jz five ;save fifth value
second : mov [1500],al ;save input at [ds:1500]
cmp al ,1
jz prime ;"1" is a prime number
mov dh,al ;save al in dh
step_2: mov al,dh
mov ah,0 ;re-initializing input
mov dh ,al
dec [1500] ;"input-1"
div [1500]
mov bx ,[1500]
cmp bx,2
jz prime ;number is prime
jnz step_2
last : mov ah ,2 ;display number of prime inputs
int 21h
hlt
one: mov [100] ,al ;save inputs in array starting [ds:100 ]
jmp second
two :mov [101],al
jmp second
three :mov [102] ,al
jmp second
four :mov [103] , al
jmp second
five :mov [104] ,al
jmp second
prime :inc dl
not_prime : loop first
jmp last
ret

Additional Details
objective of it is to :
take 5 bytes as input from keyboard and save them in an array inside memory ,count the number of prime items and display their count on screen .
when excuting im receiving a dialog box saying "PROGRAM HAS RETURNED CONTROL
TO THE OPERATING SYSTEM" ... & then after i press "OK" button ... it says : "PROGRAM TERMINATED .RELOAD "
***the dialog box appears when excution arrives the "jmp second" instruction ... :S
Three answers:
Jonathan
2012-12-23 21:46:16 UTC
The message you are getting, I gather from searching with google, comes from EMU8086. That's a low cost emulator, I gather. I'm not buying it, though, just to test things. It appears to be in wider use in Malaysia. Are you from there?



Your description includes "count the number of prime items." Reading this and taking it together with your "take 5," my first interpretation is that you really want to allow 5 numbers to be entered, not 5 bytes, and that you want a count of the primes found among them.



But the code actually uses DOS Function 1, which gets a single keyboard character. And sometimes, if a special function key or arrow key or something like that is hit, the first call to it returns a 0 and you have to call it again to get the extended code. You might be wanting to accept the keyboard codes as "items" and want to test those values for "primality." But that seems very strange to me. And it's even stranger still to see the AAA instruction being used right after a Function 1 call to DOS.



All this leads me to say I don't know what you really want to do. So I can't help. If you do a better job of explaining your goals, I'll give it a shot helping you. But my current confusion prevents me from trying, right now.
2012-12-23 22:04:10 UTC
Before anything else, I would recommend that you put address labels on their own line, because doing that makes the code easier to read. Also, I recommend using more descriptive labels.

Example:



decrement_loop:

sub cx,1

jz mylabel



There's several things that you could do better with your program.

1) You had better add input checking code. DOS function 1 has a quirk to it. If the user enters a keystroke that is not an ASCII character, then the function returns 0 in AL. The next call to function 1 will return the pseudo-scan code of the key that was pressed. So, always check to see if AL=0. If so, then notify the user with an error message and call DOS function 1 again in order to get rid of the pseudo-scan code byte before getting input again from the user. By the way, function 1 only gets a single keystroke at a time, so I'm assuming that the user will only input a single digit number.



2) Add a check to see if the user actually entered a digit between 0-9. The ASCII numbers for 0-9 is 48-57 (decimal). So, subtract 48 from AL and jump to error handling code if the number is greater than 9. If no error, then store the number in the array. By the way, don't use the AAA instruction; Subtract by 48 instead.



3) Your input loop is a complete mess and incorrect. After getting valid input, put the byte in an array and then repeat the input loop 4 more times. (It is more efficient to use only 1 loop and not use an array, but the instructions say to use an array, therefore you need 2 loops.) After that, you should check for prime numbers within the array by using another loop. The 4 single digit prime numbers are 2, 3, 5, and 7.



I don't even want to decipher your code because you're making the CPU jump all over the place. It's about as bad as some stupid high level programmer who writes too many unnecessary procedures. (That covers almost every programmer these days!)



I don't want to do your homework for you, so here's the general idea (using the MASM assembler) with some code left out:



.data ;program data

myarray db 5 dup (?) ;allocate 5 byte uninitialized array



.code ;program code starts here

mov si, offset myarray ;use SI to point to start of myarray

mov cx,5 ;input loop counter



input_loop:

mov ah,1

int 21h

(check AL for 0, jump to error code if it so)

sub al,48

(jump to error code if AL > 9)

mov byte ptr [si],al ;save AL in array

inc si ;increment array pointer

dec cx ;decrement loop counter

jnz input_loop ;if CX is not 0, jump back to input_loop

;at this point, the input loop is done

mov cx,5 ;CX is our loop counter again

mov si, offset myarray ;set SI to start of myarray



prime_loop:

mov al,byte ptr ds:[si] ;put byte from myarray into AL

cmp al,2

je primenumber ;jump if it's a prime number

(check for other numbers)

inc si ;increment myarray pointer ;execution continues here if no prime number

dec cx ;decrement counter

jnz prime_loop

(end program)



primenumber:

add al,48 ;convert AL into ASCII number

(print value in AL)

inc si ;increment myarray pointer

dec cx ;decrement loop counter

jnz prime_loop

(code for ending program, or jump to code that ends program)

(I didn't make the 'primenumber' code into a procedure because my code is more efficient.)
amaran
2016-08-08 11:36:59 UTC
What assembly language is that this? 2nd. What's incorrect with it? How have got to any person aside from anybody who's informed on this language admire? What is the intent? What's the error you possibly receiving?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...