Author Topic: URGENT HELP for newbie plzz  (Read 6544 times)

nobody

  • Guest
URGENT HELP for newbie plzz
« on: November 05, 2007, 02:32:48 AM »
hey,
i am very fresh into assembly and i want to know how to code the following program in nasm:
i have 2 data A and B .A indicates non-negative numbers or grades B indicates the 3 digit positive ID number for each of these numbers of A.I have to find what ID number(B) gets the higher grade (A), and which gets the second higher?
I have A is these numbers= 10,50,40,60,35,78,90,45,21,23
B=211,222,233,244,255,266,277,288,299,300
how do i do that? thx for ur help coz i really am fresh into nasm...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: URGENT HELP for newbie plzz
« Reply #1 on: November 05, 2007, 08:25:01 AM »
Kind of an algorithmic question, rather than a Nasm question... so ask Al Gore! :)

Seriously, if you're as new to Nasm as that, this is too hard for you to start with. The obvious approach would be to sort the grades first. You probably aren't ready to tackle that(?). Finding the highest grade in the list isn't too difficult - keeping track of which student ID it goes with complicates it, and finding next-highest too...

Anyway... first you'll need a couple of arrays for your data. I'd suggest giving them more meaningful names than "A" and "B"... The grades given look like they're on a scale of 0 to 100(?), so they'll fit in a byte.

grades db 10, 50, 40, ... ; ten of 'em

Student IDs are three digits, we're told, but they won't fit in a byte. We'll need at least a word array. Might as well make it dword, in case we're at a big school (although "three digits" rules that out).

students dd 211,222,233, ... ; ten of 'em

Fetch each of the grades in turn, comparing it against the "current maximum". If it's higher, make it our current maximum, and save the index into the array, so we can tell which student it belongs to.

My first attempt at this tried to find "second place" in the same loop - what I wound up with worked for *this* data, but screwed up if the highest grade was first in the list. This attempt just duplicates the "find highest" loop with a second copy, this time skipping over the "highest" we previously found. Crude, but probably good enough for a beginner assignment...

This is for Linux, and will want the "display" and "exit" routines changed for Windows (dos?)... whatever platform you're on. If this is for dos, the addressing mode will have to be changed to find the student ID(s). (mov bx, [blah_index] / shl bx, 1 / mov ax, [bx] - this assumes a word array for students IDs!) Maybe give you some ideas how to approach it...

Best,
Frank

; nasm -f elf grades.asm
; ld -o grades grades.o

global _start

section .data
    grades db 10, 50, 40, 60, 35, 78, 90, 45, 21, 23
    numgrades equ $ - grades

students dd 211, 222, 233, 244, 255, 266, 277, 288, 299, 300

section .bss
    maxgrade resb 1
    secondgrade resb 1
    maxgrade_index resd 1
    secondgrade_index resd 1

section .text
_start:

; find highest grade
    xor ebx, ebx
    mov ecx, numgrades
.top:
    mov al, [grades + ebx]
    cmp [maxgrade], al
    jae .notmax

mov [maxgrade], al
    mov [maxgrade_index], ebx

.notmax:
    inc ebx
    loop .top

; do it again for the silver
    xor ebx, ebx
    mov ecx, numgrades
.top2:
    cmp [maxgrade_index], ebx
    je .notsecond  ; skip the highest one we found

mov al, [grades + ebx]
    cmp [secondgrade], al
    jae .notsecond

mov [secondgrade], al
    mov [secondgrade_index], ebx

.notsecond:
    inc ebx
    loop .top2

; display results - you'll want it more verbose than this. :)

mov ebx, [maxgrade_index]
    mov eax, [students + ebx * 4]
    call showeaxd
    mov ebx, [secondgrade_index]
    mov eax, [students + ebx * 4]
    call showeaxd

; exit to OS
    mov eax, 1
    int 80h

;---------------------------------
showeaxd:
    push eax
    push ebx
    push ecx
    push edx
    push esi

sub esp, 10h
    lea ecx, [esp + 12]
    mov ebx, 10
    xor esi, esi
    mov byte [ecx], 0
.top:
    dec ecx
    xor edx, edx
    div ebx
    add dl, '0'
    mov [ecx], dl
    inc esi
    or eax, eax
    jnz .top

mov edx, esi
    mov ebx, 1
    mov eax, 4
    int 80h

add esp, 10h

pop esi
    pop edx
    pop ecx
    pop ebx
    pop eax

ret
;---------------------------------