|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
[Assembler 8086] Determinare numero negativo
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?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Mar 2008
Messaggi: 267
|
La soluzione più semplice sarebbe un call alla funzione "atol", che ti converte la stringa in numero:
Codice:
push offset stringa call atol Codice:
invoke atol, addr stringa 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. Ultima modifica di Supdario : 14-11-2010 alle 12:54. |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Quote:
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Mar 2008
Messaggi: 267
|
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).
|
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Quote:
Comunque ho risolto nel seguente modo: Codice:
;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
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:53.




















