Hello,

I have to write an assembly language program that finds out maximum number from an array of ten numbers.

I using nasm, i want this program to be checked, if someone could help.




mov bx,2
mov dx,

jmp start


Array:
2,8,6,1,7,4,1,14,5,9

max:
dw 0

Maximum:
mov dx,

Loop:
cmp bx,20
add bx,2

start:
cmp ,dx
jnbe Maximum
cmp bx,20
jnae Loop


mov ax,0x4c00
int 0x21
Posted on 2009-11-01 05:44:52 by Emerald
Checked:
Emerald.nasm:10: error: label or instruction expected at start of line
Posted on 2009-11-01 05:49:18 by baldr
Can you point out where i am wrong?
Posted on 2009-11-01 06:19:42 by Emerald

Can you point out where i am wrong?


Array is a label, but you didn't specify any data type for the subsequent elements.

Take a look at how you declared the max "variable" in your code, and go from there.
Posted on 2009-11-01 10:19:25 by SpooK
I have again written code for finding the maximum number out oan array of 10 numbers. I have highlighted the lines with error in syntax. Kindly correct





jmp start


Array:
dw 2,8,6,1,7,4,1,14,5,9

noAction:
mov ax,0x4c00
int 0x21


Loop:
cmp bx,10
ja increment
jb noAction

increment:
add bx,1

Maximum:
cmp dx,
ja SetMax
jbe Loop


SetMax:
mov dx,
jmp Loop
return

start:
mov bx,0
mov dx,
call Maximum


mov ax,0x4c00
int 0x21


the algorithm i have used is as follows


function Maximum(Array){
max=Array[0]
for(i=1;i<10;i++){
if(array>max)
{
max=Array;
}
return max
}
Posted on 2009-11-04 12:27:57 by Emerald
Sorry the above code could not show the error lines, i will comment those lines again




jmp start


Array:
dw 2,8,6,1,7,4,1,14,5,9

noAction:
mov ax,0x4c00
int 0x21


Loop:          ; Error
cmp bx,10
ja increment
jb noAction

increment:
add bx,1

Maximum:
cmp dx,
ja SetMax
jbe Loop  ;Error


SetMax:
mov dx,
jmp Loop  ;Error
return

start:
mov bx,0
mov dx,
call Maximum


mov ax,0x4c00
int 0x21


All the errors are syntax errors. Kindly correct them.
Posted on 2009-11-04 12:37:32 by Emerald
LOOP is an x86 instruction, therefore you cannot use it as a label.
Posted on 2009-11-04 14:50:40 by SpooK
Furthermore, "return" is not an x86 instruction! Nasm is treating it as a label (may or may not give you a warning, depending on Nasm version). Doesn't get executed, in any case.

Intel syntax (Nasm syntax, more or less) has the operands in "reverse" order (depending on how you look at it). This can be confusing...

"mov ax, bx" moves bx into ax, not ax into bx. So, if you've got:

cmp ax, bx
ja foo

Does it jump if ax is "above", or bx? Answer: if ax is "above". You get this right when you're comparing for "max", but not when you're comparing bx.

How large are the items in your array? So how much do you want to add to bx to get to the next one?

Keep plugging, you're getting there!

Best,
Frank

Posted on 2009-11-04 15:43:02 by fbkotler

Does it jump if ax is "above", or bx? Answer: if ax is "above". You get this right when you're comparing for "max", but not when you're comparing bx.


Are you sure about this ? because in
cmp ax,bx
ja Loop1
.....our teacher told us that, the loop will execute if bx is above ax.  Secondly you said that i have done the comparison correctly while comparing for max and its wrong when comparing bx. I re-looked at it and found that i have used the same logic in both cases.

I have deleted return from my subroutine, because we know that with 'call' , return is not obligatory. Also i have replaced the 'Loop' with 'loop1' Now the program gets compiled easily, but when i run it in debugger, there is no change in the registers after executing the program step by step. The registers remain at the same values thoroughout the program.

I even reversed the 'ja' in bx comparison as you mentioned, but still debugger shows no change in registers. I am so confused about this. Kindly help!
Posted on 2009-11-05 16:28:54 by Emerald
I stand corrected - you do use the same logic after the "cmp" in both places. Sorry 'bout that.

As to which is right... we can "look it up" or "ask the expert"... I prefer "do the experiment". Write yourself some "junk code", assemble it, and run it in the debugger. Or write it so it outputs some result...

org 100h

  mov ax, 1
  mov bx, 2
  cmp ax, bx
  ja did_jump

  mov al, 'N'
  int 29h
  jmp exit

did_jump:
  mov al, 'Y'
  int 29h

exit:
  ret

Fiddle with the numbers, and with the condition code - try "jb", "jna", "jc"... Might as well try "jg" and "jl", too (these treat the numbers as signed... so 0FFFFh is "less" than 1). You might learn something - probably want to RTFM, too - some things are hard to determine by "trial and error"...

I have no explanation why the registers don't change in the debugger. Lessee, we:

jmp start

That should change ip, but no other register. Then, at "start:"...

mov bx,0

bx was probably already zero, so this may not do anything obvious.

mov dx,

That should make dx 2... if it wasn't already...

call Maximum

Besides changing ip, this should change sp - "call" saves the return address on the stack. It's true that "with 'call' , return is not obligatory", but it's a pretty good idea to keep "call"s and "ret"s matched... unless you have something "tricky" in mind.

The "junk code" above ends with "ret", even though there's no "call". The reason this works is because, when dos loads a .com file, it pushes a (word) zero on the stack. "ret" fetches it's address to return to from the stack, so we go to cs:0... where we will find "cd 20" - int 20h - in the "program segment prefix" (PSP) which dos puts there. I probably shouldn't have showed you that. The int 21h/4Ch you're using is "better".

I probably shouldn't have showed you int 29h, either. It's documented as "internal". We're probably not "supposed" to use it. But it's simple, and it works...

Try your code in a debugger again. If the registers really don't change, "something's wrong"! What will we do if there's a bug in the debugger??? Hopefully, you just need a little experience learning how to use it. If you're using DEBUG, there's a "smarter" debugger from David Lindauer - GRDB. Latest version I've got (I think David's run out of time to work on it)...

http://home.myfairpoint.net/fbkotler/grdb194.zip

Best,
Frank

Posted on 2009-11-05 17:54:23 by fbkotler
Correction:

http://home.myfairpoint.net/fbkotler/grdbdl94.zip

It's a really nice debugger...

Best,
Frank

Posted on 2009-11-05 18:06:45 by fbkotler