5 Replies - 701 Views - Last Post: 16 May 2013 - 07:29 PM

#1 domenico  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 21-July 12

Finding the minium value of an array

Posted 11 May 2013 - 09:16 AM

Hi guys,
this is one of my first assembly code, what do you think about it?
Specifically,
1 Is there a better way I can go thourgh array?
2 I'm using SI to store the array address because I've seen it's a common paradigm to do that. But why to bother? Can't I just use 'VECTOR' instead?
3 Other general advice? (It's my really first code...)

.
org     100h

.MODEL      SMALL
.DATA
    VECTOR      DW  3,4,2,0,54,-2,3,4
    MIN         DW  ?

.CODE 
.STARTUP
    XOR         AX,AX           ;counter
    LEA         SI,VECTOR       ;store vector address in SI
    MOV         BX,[SI]         ;BX is the minium, let's suppose it's the first element here
    ADD         SI,2            ;I want to go through the array 2byte at once
    INC         AX              ;scanned elements in AX
    
TESTMIN:
    CMP         BX,[SI]
    JL          GOAHEAD
    MOV         BX,[SI] 
    
GOAHEAD:    
    ADD         SI,2
    INC         AX
    CMP         AX,8
    JL          TESTMIN            
    
ret


Thank you guys :)

This post has been edited by domenico: 11 May 2013 - 09:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the minium value of an array

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Finding the minium value of an array

Posted 11 May 2013 - 07:35 PM

Quote

3 Other general advice? (It's my really first code...)

Learn 32 bit code!!!! Less instructions and easier to use:

    mov     ecx, 7
    lea     esi, VECTOR
    mov     ax, [esi + 2 * ecx]
    dec     ecx
       
TESTMIN:
    cmp     ax, [esi + 2 * ecx]
    cmovnl  ax, [esi + 2 * ecx]
        
GOAHEAD:    
    dec     ecx
    jnz     TESTMIN  
    ret


16Bit:
mov     ax, @data
    mov     ds, ax
	
    mov     di, 7
    lea     si, VECTOR
    mov     ax, [si]
    add     si, 2
    dec     di
   
TESTMIN:
    cmp     ax, [si]
    jl      GOAHEAD
    mov     ax, [si]
    
GOAHEAD:    
    add     si, 2
    dec     di
    jnz     TESTMIN
     
	mov ax, 4c00h
	int 21h


Either way, the lowest value is in ax which is called the return value. It is standard to put the return value in EAX/AX
Was This Post Helpful? 1
  • +
  • -

#3 domenico  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 21-July 12

Re: Finding the minium value of an array

Posted 12 May 2013 - 03:20 AM

I'll dive into 32bit and even 64bit programming, I think, once I take my microprogramming exam :)
Anyway,
1 What are the first two instruction in your 16bit code?
2 Don't you think looping like this
mov     ax, @data
    mov     ds, ax
	
   mov     cx, 7 ; //<------
   lea     si, VECTOR
    mov     ax, [si]
    add     si, 2
    dec     cx ; //<------
  
TESTMIN:
    cmp     ax, [si]
    jl      GOAHEAD
    mov     ax, [si]
    
GOAHEAD:    
    add     si, 2
    loop   TESTMIN; //<------
    
	mov ax, 4c00h
	int 21h


Would be quicker?
3 Can't I substitute
mov ax, 4c00h
	int 21h

with ret even in 16bit mode? (emu8086 doesn't seem to refuse that, that's why I'm asking)

Quote

Either way, the lowest value is in ax which is called the return value. It is standard to put the return value in EAX/AX

Thank you for that! :)

This post has been edited by domenico: 12 May 2013 - 03:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Finding the minium value of an array

Posted 12 May 2013 - 07:05 PM

Quote

1 What are the first two instruction in your 16bit code?

16Bit code, uses segments and you must set up the Data Segment register to point to your data section in order to get correct results.

Quote

2 Don't you think looping like this

No, the LOOP instruction is an old "Complex" instruction (A complex instruction is one that does more than one thing at a time). It was fast on the original Pentium, but much slower on newer CPUs. Manual coded loops are by far better.

Beginner - Rule #3

Quote

3 Can't I substitute

No, ret is NOT the proper way to exit your program/return control back to the OS.
DOS - Int 21/AH=4Ch
Windows - ExitProcess
*NIX = Int 80H/eax=sys_exit / syscall rax= 60

ret might work in the emulator because it is well an emulator and protects you. It also might work on your OS today but not tomorrow. The main reason not to use ret, is it does not allow the OS to clean up any open handles and memory correctly. Learn the right way today, and you will have less problems in the future!
Was This Post Helpful? 1
  • +
  • -

#5 domenico  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 21-July 12

Re: Finding the minium value of an array

Posted 16 May 2013 - 09:25 AM

I can't see what you're saying here.
Can you write the relative instruction as you would give them to the assembler?

View PostGunnerInc, on 12 May 2013 - 07:05 PM, said:

No, ret is NOT the proper way to exit your program/return control back to the OS.
DOS - Int 21/AH=4Ch
Windows - ExitProcess
*NIX = Int 80H/eax=sys_exit / syscall rax= 60

------

Other question.
mov     ax, @data
mov     ds, ax



Do I have to use that in the .code only when I use .data or it's just smth that goes there anyway?

This post has been edited by domenico: 16 May 2013 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Finding the minium value of an array

Posted 16 May 2013 - 07:29 PM

No, do some research on your own - look up Int 21/AH=4Ch since you are using 16bit code - http://ctyme.com/rbrown.htm

32 bit apps, use ExitProcess - that can be found on MSDN. Don't think you will be hitting *nix any time soon.

If you are going to access vars in the data section, then you must set up the segment registers, ds is the Data Segment register.

And I will say again, RET is NOT the proper way to exit a program, smack the teacher that is teaching that!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1