PDA

View Full Version : [Assembler 8086] Determinare numero negativo


kwb
13-11-2010, 17:09
Ciao..
Devo scrivere un programma che è in grado di determinare quanti valori di un array sono negativi.
Il problema che sorge è che non ho la più pallida idea di come inserire nel vettore i numeri negativi... Certo potrei tranquillamente manipolare i codici ascii e far si che quando trova un '-' allora deve avanzare ancora di uno per prendere l'intero numero, ma non mi pare una soluzione buona...
Volevo cercare di far qualcosa con la rappresentazione in complemento a due, però i numeri devono essere decimali e non so come convertirli in binario...
Suggerimenti?

Supdario
14-11-2010, 12:47
La soluzione più semplice sarebbe un call alla funzione "atol", che ti converte la stringa in numero:

push offset stringa
call atol

O in sintassi MASM32:

invoke atol, addr stringa

Successivamente avrai sul registro "eax" il numero convertito.

PS: Se usi un assembler a 16bit è ovvio che devi usare la funzione "atoi" e prendere il valore dal registro ax. Inoltre devi includere la relativa libreria di Windows.

kwb
14-11-2010, 14:44
La soluzione più semplice sarebbe un call alla funzione "atol", che ti converte la stringa in numero:

push offset stringa
call atol

O in sintassi MASM32:

invoke atol, addr stringa

Successivamente avrai sul registro "eax" il numero convertito.

PS: Se usi un assembler a 16bit è ovvio che devi usare la funzione "atoi" e prendere il valore dal registro ax. Inoltre devi includere la relativa libreria di Windows.
Ma atoi non è una funzione di C??

Supdario
14-11-2010, 16:12
No, quasi tutte le funzioni native del C sono chiamate al kernel del sistema operativo (che sia Windows, Linux o altro). Anche "malloc" ad esempio è una chiamata al sistema operativo, che si occupa di trovare spazio libero nella memoria ed allocarlo, e restituisce il puntatore. Tutte queste funzioni si trovano in liberie statiche o dinamiche. Nello specifico, queste funzioni sono contenute in msvcrt.lib o msvcrt.dll (che contiene printf, system, malloc, scanf, ed altro).

kwb
14-11-2010, 17:31
No, quasi tutte le funzioni native del C sono chiamate al kernel del sistema operativo (che sia Windows, Linux o altro). Anche "malloc" ad esempio è una chiamata al sistema operativo, che si occupa di trovare spazio libero nella memoria ed allocarlo, e restituisce il puntatore. Tutte queste funzioni si trovano in liberie statiche o dinamiche. Nello specifico, queste funzioni sono contenute in msvcrt.lib o msvcrt.dll (che contiene printf, system, malloc, scanf, ed altro).
Ah ho capito, beh buono a sapersi! Potrà tornarmi utile in futuro...
Comunque ho risolto nel seguente modo:

;You are asked to develop an 8086 assembly program that counts the number of both positive and negative
;elements of an integer vector.

.MODEL small
.STACK
MAX EQU 10 ;Number of elements in the array
CR EQU 13 ;Carriage Return, decimal
LF EQU 10 ;Line Feed, decimal

.DATA
posElem DB 48 ;Number of positive elements, starts from 48 because it's 0 in ascii table
;Decimal notation
negElem DB 48 ;As above, but for negative elements
array DB -1,2,5,4,2,6,9,-5,-4,-9 ;Our array
msgPos DB "Number of positive elements: $" ;Print message for positive
msgNeg DB "Number of negative elements: $" ;Print message for negative

.code
.startup
XOR DX,DX ;Reset some registers
XOR AX,AX
XOR DI,DI
MOV CX,MAX ;And put max number of elements in CX

findNeg: MOV AL,array[DI]
AND AL,80H ;Check if the number is positive, if it is, then we get 00000000 in our register
CMP AL,0 ;Otherwise we get 10000000
JE findPos ;If AL == 0 then goto findPos
INC negElem ;Otherwise we have a negative element, so we increase the counter
INC DI ;Index increased
LOOP findNeg ;Loop until CX == 0
JMP print ;Then print results
findPos: INC posElem ;If we are here we found a positive element, we increase counter
INC DI ;And index
LOOP findNeg ;Loop until CX == 0

print: MOV AH,9 ;Prepare for printing a string
LEA DX,msgPos ;Load effective address of msgPos string, this is the same as MOV DX, OFFSET msgPos
INT 21H ;Print

MOV AH,2 ;Prepare for printing a single char
MOV DL,posElem ;Put posElem in DL for printing
INT 21H ;Print

MOV DL,CR ;Print a CR
INT 21H
;and
MOV DL,LF ;a LF
INT 21H

MOV AH,9 ;Print the string for negative numbers
LEA DX,msgNeg
INT 21H

MOV AH,2 ;Print the number of negative elements
MOV DL,negElem
INT 21H

.EXIT
END