How to print an equilateral triangle based off user input in NASM? - linux

I'm trying to print an equilateral triangle made of * in NASM based off of user input that can be from numbers 01 to 99. So if the user inputs 99 the bottom row of the triangle will have 99 stars, then the row above that would have 97 stars, then above that row 95 stars, etc. but it is only making a rectangle with the same width and length. How can I change my code so that it will print an equilateral triangle and not a rectangle? Thanks for the help.
section .data
star: db '*', 1
starLen1: equ $-star
;endl
newLineMsg: db 0xA, 0xD
newLineLen: equ $-newLineMsg
section .bss
TriangleSize resb 1 ;Holds width of triangle
TriangleSize2 resb 1
spacebewteenvalues resb 1 ;take space bwteen values
loopcounter1 resb 2 ;hold count for first loop
loopcounter2 resb 2 ;hold count for 2nd loop
answer2 resb 2 ;hold first digital after times 10 for second input
answer3 resb 2 ;hold value after plus 2nd digit
section .text
global _start
_start:
mov eax,3
mov ebx,0
mov ecx,TriangleSize
mov edx,1
int 80h
mov eax,3
mov ebx,0
mov ecx,TriangleSize2
mov edx,1
int 80h
;sub ascii from each digit
sub [TriangleSize], byte '0'
sub [TriangleSize2], byte '0'
;multiply first digit by 10
mov al, [TriangleSize]
mov bl, 10
mul bl
;move into variable
mov [answer2], ax
;add 2nd digit
mov al, [answer2]
add al, [TriangleSize2]
;move both digit into variable
mov [answer3], al
;convert to decimal
add [answer3], byte '0'
;reset loop
mov [loopcounter1], byte '0'
mov [loopcounter2], byte '1'
;Start to cout *
jmp TriFunction1
;outputs first row
TriFunction1:
;move counter into reigster
mov al, [loopcounter1]
;compare row length then jump to next row
cmp al, [answer3]
je CoutNewline ;endl
;cout *
mov eax,4
mov ebx,1
mov ecx,star
mov edx,1
int 80h
;inc the loop counter
add [loopcounter1], byte 1
;jump back to beginning
jmp TriFunction1
;goes to next row
TriFunction2:
;move 2nd loop counter into register
mov al, [loopcounter2]
;compare
cmp al, [answer3]
je end ;when rectangle has finished drawing go back to main
add [loopcounter2], byte 1
;output the next row of *
jmp TriFunction1
;endl
CoutNewline:
;out \n
mov edx, newLineLen
mov ecx, newLineMsg
mov ebx, 1
mov eax, 4
int 0x80
;reset loop
mov [loopcounter1], word '0'
;check for next row
jmp TriFunction2
end:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h;
;takes space between user input
takespacebetweennumbers:
mov eax,3
mov ebx,0
mov ecx,spacebewteenvalues
mov edx,1
int 80h
ret ;return back

Try to draw the expected output and then analyze how to achieve the result.
It's always better to divide a complex task into small steps.
I replaced the tail of your code from the line ;move both digit into variable:
;move both digit into variable
mov [answer3], al
; AL is now binary size of the triangle side (00..99). Examples:
; AL=07 Spaces Asterixes
; * 3 1
; *** 2 3
; ***** 1 5
;******* 0 7
; AL=08 Spaces Asterixes
; ** 3 2
; **** 2 4
; ****** 1 6
;******** 0 8
SECTION .data
Space DB ' '
Asterix DB '*'
NewLine DB 10
SECTION .bss
Spaces RESB 1
Asterixes RESB 1
Rows RESB 1
SECTION .text
; Calculate Spaces and Asterixes from triangle Size in AL.
CMP AL,1
JNA end
DEC AL
SHR AL,1 ; AL=(Size-1)/2, CF=1 if Size was even.
MOV [Spaces],AL
MOV AH,1
ADC AH,0 ; Start the 1st row with 1 or 2 asterixes.
MOV [Asterixes],AH
INC AL
MOV [Rows],AL
NextLine:
CALL PrintSpaces
CALL PrintAsterixes
CALL PrintNewLine
DEC BYTE [Spaces]
ADD BYTE [Asterixes],2
DEC BYTE [Rows]
JNZ NextLine
end:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h;
PrintSpaces:
LEA ECX,[Space] ; Address of a character to print.
MOVZX EBP,BYTE [Spaces] ; How many times.
JMP Print
PrintAsterixes:
LEA ECX,[Asterix] ; Address of a character to print.
MOVZX EBP,BYTE [Asterixes] ; How many times.
JMP Print
PrintNewLine:
LEA ECX,[NewLine] ; Address of a character to print.
MOV EBP,1 ; How many times.
Print:
TEST EBP,EBP ; How many times to repeat the character.
JZ No
MOV EBX,1 ; Standard output handle.
MOV EDX,1 ; Print one character addressed by ECX.
NextChar:
MOV EAX,4 ; Invoke kernel function sys_write.
INT 80h
DEC EBP
JNZ NextChar ; Repeat EBP times.
No:RET
Modification which will draw a diamond shape instead of triangle:
; AL is now binary size of the diand width side (00..99). Examples:
; AL=07 Spaces Asterixes
; * 3 1
; *** 2 3
; ***** 1 5
;******* 0 7
; ***** 1 5
; *** 2 3
; * 3 1
; AL=08 Spaces Asterixes
; ** 3 2
; **** 2 4
; ****** 1 6
;******** 0 8
; ****** 1 6
; **** 2 4
; ** 3 2
SECTION .text
; Calculate Spaces and Asterixes from triangle Size in AL.
CMP AL,1
JNA end
DEC AL
SHR AL,1 ; AL=(Size-1)/2, CF=1 if Size was even.
MOV [Spaces],AL
MOV AH,1
ADC AH,0
MOV [Asterixes],AH
INC AL
MOV [Rows],AL
UpperLines: ; The upper half of the diamond.
CALL PrintSpaces
CALL PrintAsterixes
CALL PrintNewLine
DEC BYTE [Spaces]
ADD BYTE [Asterixes],2
DEC BYTE [Rows]
JNZ UpperLines
SUB BYTE [Asterixes],2 ; Rollback the middle line of the diamond.
INC BYTE [Spaces]
LowerLines: ; The lower half of the diamond.
SUB BYTE [Asterixes],2
INC BYTE [Spaces]
CALL PrintSpaces
CALL PrintAsterixes
CALL PrintNewLine
CMP BYTE [Asterixes],2
JNB LowerLines
end:

Related

Addition and Subtraction in Nasm

The code just prints out nothing what would I do for the output that will be 150 and 48? I'm practicing nasm
section .data
num1 db 99
num2 db 51
result db 0
section .text
global _start
_start:
; Addition
mov al, [num1]
add al, [num2]
mov [result], al
call display_result
; Subtraction
mov al, [num1]
sub al, [num2]
mov [result], al
call display_result
; Exit program
mov eax, 1
int 0x80
display_result:
; Display result
mov edx, 1
mov ecx, [result]
mov ebx, 1
mov eax, 4
int 0x80
; Display newline
mov edx, 1
mov ecx, 10
mov ebx, 1
mov eax, 4
int 0x80
exit:
mov eax, 1
int 0x80`
I expect for my coding to print the Addition and Subtraction of Num1 and Num2 but it is not printing what would I do for it to print?
See DIV instruction, I think it will best fit your needs. Just divide number by 10 and add the remainder to ASCII '0' value and your result will be one digit from the back of the number you are trying to print. For example: 157 will produce 7 remainder first time, 5 second time and then you'll be left with 1 itself. Hope you get the idea.

NASM Assembly - Multiply Two Numbers Input by the User

I'm super new to assembly, and I've spent literally hours trying to figure this out. The program I'm making is supposed to take two numbers from zero to nine, then multiply them and show the correct answer (ex: 9 times 3 is 27). I need to have the 2 numbers be input from the user with the notation of "space number space number" (an example could be " 5 3"). I tried making the code without input (hardcode the numbers) and that works, but when I try to make the numbers come from input, the result is wrong. The program is supposed to take two numbers from zero to nine, then multiply them. I really can't figure out what's wrong...Any help at all would be very much appreciated.
When I enter: " 5 3" it outputs "? 4" (it should be 15)
When I enter: " 9 3" it still outputs "? 4" (it should be 27)
When I enter: "5 3" it outputs "?6"
When I enter: "9 3" it outputs "??4"
When I check var1 and var2 (by making it print them after them being read), the numbers are correct. As in, if " 5 3" is entered var1 does print 5 and var2 does print 3. Even if the spaces are changed, var1 and var2 print the correct numbers (so if I enter "5 3" var1 is still 5, and var2 is still 3).
My professor just explained what to do to get input, but I'm not 100% sure how it works. I find it odd that the output is different depending on where the spaces are, as var1 and var2 should both be accepting 2 bytes.
My code that's incorrect:
;this program multiplies two numbers input by the user, being able to solve with two digits
;the notation for the input should be space number space number (ex:" 9 3")
section .data
section .bss
var1 resb 2
var2 resb 2
result resb 2
tensdigit resb 2
onesdigit resb 2
section .text
global _start
_start:
;reads & saves user input for var1
mov eax, 3
mov ebx, 0
mov ecx, var1
mov edx, 2 ;2 bytes of info
int 80h
;reads & saves user input for var2
mov eax, 3
mov ebx, 0
mov ecx, var2
mov edx, 2 ;2 bytes of info
int 80h
mov al, [var1]
; sub al, '0' ;not sure if this is needed or not
mov bl, [var2]
; sub bl, '0'
;multiply bl by al
mul bl
; sub ax, '0'
mov [result], ax
;divide by ten
mov ax, 0 ;set the register to 0! Very important to avoid errors
mov ax, [result]
mov bx, 10
div bx ;divide by ten
mov [tensdigit], al ;this should have 1 if using 12 (2 if 24, etc)
mov ax, [result]
sub ax, 10
mov [result], ax ;move new number into result
mov al, [result]
mov bl, 10
div bl
;find the remainder
mov [onesdigit], ah
add [tensdigit], word '0' ;add 0 to ensure it's considered a number
add [result], word '0'
add [onesdigit], word '0'
;print tensdigit
mov eax, 4
mov ebx, 1
mov ecx, tensdigit
mov edx, 2
int 80h
;print onesdigit
mov eax, 4
mov ebx, 1
mov ecx, onesdigit
mov edx, 2
int 80h
;system exit
mov eax,1
mov ebx,0
int 80h;
This is my code when the numbers are hardcoded (this one multiplies the numbers correctly):
;this program adds two numbers, being able to solve with two digits
section .data
section .bss
var1 resb 2
var2 resb 2
result resb 2
tensdigit resb 2
onesdigit resb 2
section .text
global _start
_start:
;setup the variables
mov [var1], word 9
mov [var2], word 3
mov al, [var1]
mov bl, [var2]
mul bl
mov [result], ax
;divide by ten
mov ax, 0 ;set the register to 0! Very important to avoid errors
mov ax, [result]
mov bx, 10
div bx ;divide by ten
mov [tensdigit], al ;this should have 1 if using 12 (2 if 24, etc)
mov ax, [result]
sub ax, 10
mov [result], ax ;move new number into result
mov al, [result]
mov bl, 10
div bl
;find the remainder
mov [onesdigit], ah
add [tensdigit], word '0' ;add 0 to ensure it's considered a number
add [result], word '0'
add [onesdigit], word '0'
;print tensdigit
mov eax, 4
mov ebx, 1
mov ecx, tensdigit
mov edx, 2
int 80h
;print onesdigit
mov eax, 4
mov ebx, 1
mov ecx, onesdigit
mov edx, 2
int 80h
mov eax,1
mov ebx,0
int 80h;

Adding 2 inputted numbers in Assembly using NASM

I have done my best to explain all of my thought process when adding these two numbers. However, upon running the resulting executable I end up with
"Sum is: j" which is clearly wrong. Additionally, it seems that no matter which inputs I give the sum stays as "j" so there must be something awfully wrong.
I believe this code should work but my understanding is clearly flawed.
Where should I start in fixing this? I just recently began learning assembly.
section .data ;line 1
msg db "Sum is: "
len equ $ - msg
section .bss
num1 resb 1
eol1 resb 1
num2 resb 1
eol2 resb 1
sum resb 2
section .text
global _start
print_int:
mov eax, 4 ;defining routine print_int
mov ebx, 1 ;file descriptor (stdout)
int 0x80 ;system call number (sys_write)
ret ;return back
_start:
;Read and store user input for num1
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2 ;2 bytes of info
int 80h
mov byte [eol1], 0xA ; value of first end of line
;Read and store user input for num2
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2 ;2 bytes of info
int 80h
mov byte [eol2], 0xA ;value of 2nd end of line
;Add num1 and num2
mov eax, num1
sub eax, '0' ;this is so that the value in 3 is set to 3 because
;'3' and '0' actually are ASCII values
mov ebx, num2
sub ebx, '0'
add eax, ebx ;Move the sum of 0x3 and 0x4 into eax
add eax, '0' ;Set eax to be the ASCII value for the result of the sum
mov [sum], eax ;Set this ascii value of the sum to sum
mov ecx, msg ;Move msg ('Sum is: ') into eax
mov edx, len ;Move len (length of msh) into edx
call print_int ; call routine print_int above
;load sum to to be printed
mov ecx, sum
mov edx, 2 ;size in bytes of sum
call print_int
mov eax, 1 ;system call number (sys_exit)
xor ebx, ebx
int 0x80 ;line 43
Your program operates on 2 single digit numbers inputted by the user.
mov eax, num1
mov ebx, num2
On NASM, this will move the address of these variables in those registers. What you want is the contents! You need to write the square brackets.
But wait - since the input has only a single byte of information, you should read the data in the byte-sized registers AL and BL.
mov al, [num1]
mov bl, [num2]
All subtractions and additions then will also have to use these smaller sizes.
sub al, '0' ;From character '0'-'9' to number 0-9
sub bl, '0'
add al, bl
add al, '0' ;From number 0-9 to character '0'-'9'
The character in AL is what you want to print. Easiest is to first append the newline 0xA in the AH register and then write AX in the memory.
mov ah, 0xA
mov [sum], ax
All the above is correct whenever the sum of the 2 single digit numbers was less than 10.
Imagine what will happen when you input e.g. numbers 5 and 8 ?
The sum (13) would require 2 characters leaving no room for the extra newline character. The largest sum will come from adding 9 and 9 (18).
Best re-define "sum" as sum resb 3.
Then write the following:
mov al, [num1]
mov bl, [num2]
sub al, '0'
sub bl, '0'
add al, bl
mov edx, 2 ;String length if single digit sum
mov ecx, sum ;Address of sum
cmp al, 10
jb SingleDigit
mov byte [ecx], '1'
inc ecx ;Move to position for the units/newline
inc edx ;String length if double digit sum
sub al, 10 ;Only keep the units
SingleDigit:
add al, '0' ;From number 0-9 to character '0'-'9'
mov ah, 0xA ;Append newline
mov [ecx], ax
mov ecx, sum
call print_int

Tower of Hanoi in assembly x86 using arrays

Hi every one I am trying to do a tower of Hanoi in assembly x86 but I am trying to use arrays. So this code gets a number from user as a parameter in Linux, then error checks a bunch of stuff. So now I just want to make the algorithm which use the three arrays i made (start, end, temp) and output them step by step. If someone can help it would be greatly appreciated. `
%include "asm_io.inc"
segment .data ; where all the predefined variables are stored
aofr: db "Argument out of Range", 0 ; define aofr as a String "Argument out of Range"
ia: db "Incorrect Argument", 0 ; define ia as a String "Incorrect Argument"
tma: db "Too many Arguments", 0 ; define tma as a String "Too many Arguments"
hantowNumber dd 0 ; define hantowNumber this is what the size of the tower will be stored in
start: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings start at
end: dd 0,0,0,0,0,0,0,0,9 ; this array is where all the rings end up at
temp: dd 0,0,0,0,0,0,0,0,9 ; this array is used to help move the rings
test: dd 0,0,0,0,0,0,0,0,9
; The next couple lines define the strings to show the pegs and what they look like
towerName: db " Tower 1 Tower 2 Tower 3 ", 10, 0
lastLineRow: db "XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX ", 10, 0
buffer: db " ", 0
fmt:db "%d",10,0
segment .bss ; where all the input variables are stored
segment .text
global asm_main ; run the main function
extern printf
asm_main:
enter 0,0 ; setup routine
pusha
mov edx, dword 0 ; set edx to zero this is where the hantowNumber is saved for now
mov ecx, dword[ebp+8] ; ecx has how many arguments are given
mov eax, dword[ebp+12] ; save the first argument in eax
add eax, 4 ; move the pointer to the main argument
mov ebx, dword[eax] ; save the number into ebx
push ebx ; reserve ebx
push ecx ; reserve ecx
cmp ecx, dword 2 ; compare if there are more the one argument given
jg TmA ; if more then one argument is given then jump Too many Argument (TmA)
mov ecx, 0 ; ecx = 0
movzx eax, byte[ebx+ecx] ; eax is the first character number from the inputed number
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
string_To_int: ; change String to int procedure
add edx, eax ; put the number in edx
inc ecx ; increase counter (ecx)
movzx eax, byte[ebx+ecx] ; move the next number in eax
cmp eax, 0 ; if eax = 0 then there are no more numbers
mov [hantowNumber], edx ; change hantowNumber to what ever is in edx
je rangeCheck ; go to rangeCheck to check if between 2-8
sub eax, 48 ; subtract 48 to get the actual number/letter/symbol
cmp eax, 10 ; check if eax is less then 10
jg IA ; if eax is greater then 10 then it is a letter or symbol
imul edx, 10 ; multiply edx by 10 so the next number can be added to the end
jmp string_To_int ; jump back to string_To_int if not done
rangeCheck: ; check the range of the number
cmp edx, dword 2 ; compare edx with 2
jl AofR ; if hantowNumber (edx) < 2 then go to Argument out of Range (AofR)
cmp edx, dword 8 ; compare edx with 8
jg AofR ; if hantowNumber (edx) > 8 then go to Argument out of Range (AofR)
mov ecx, [hantowNumber] ; move the number enterd by user in ecx
mov esi, 28 ; esi == 28 for stack pointer counter
setStart: ; set the first array the starting peg
mov [start+esi], ecx ; put ecx into the array
sub esi, 4 ; take one away from stack pointer conter
dec ecx ; take one away from ecx so the next number can go in to the array
cmp ecx, 0 ; compare ecx with 0
jne setStart ; if ecx != 0 then go to setStart loop
; This is the section where the algoritham should go for tower of hanoi
mov ecx, [hantowNumber]
towerAlgorithm:
cmp ecx, 0
jg Exit ; jump to Exit at the end of the program
dec ecx
IA:
mov eax, ia ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
AofR:
mov eax, aofr ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
TmA:
mov eax, tma ; put the string in eax
push eax ; reserve eax
call print_string ; output the string that is in eax
call print_nl ; print a new line after the output
pop eax ; put eax back to normal
add esp, 4 ; takes 4 from stack
jmp Exit ; jump to Exit at the end of the program
Exit: ; ends the program when it jumps to here
add esp, 9 ; takes 8 from stack
popa
mov eax, 0 ; return back to C
leave
ret
haha I'm doing the exact same assignment and stuck on the algorithm however though when running your code it seems to identify "too many arguments" even though only one argument is provided, consider this algorithm when dealing with arguments(don't forget ./ is considered the "first argument" since it is the zeroth argument provided):
enter 0,0
pusha
; address of 1st argument is on stack at address ebp+12
; address of 2nd arg = address of 1st arg + 4
mov eax, dword [ebp+12] ;eax = address of 1st arg
add eax, 4 ;eax = address of 2nd arg
mov ebx, dword [eax] ;ebx = 2nd arg, it is pointer to string
mov eax, 0 ;clear the register
mov al, [ebx] ;it moves only 1 byte
sub eax, '0' ;now eax contains the numeric value of the firstcharacter of string

NASM JLE: Jump if Less or Equal always evaluating to true

I have code here that reads in input to determine the dimensions of a matrix/2d array and then reads in numbers one by one. Then it is SUPPOSED to spit out the smallest number.
However my comparison operator doesn't seem to be working? I've tried putting both as registers, with different variables and so on but for some reason eax with whatever happened to be the first entry in the array is ALWAYS smaller than the next number, even if this is clearly not the case.
So it always skips reassignment.
Code: Just skip straight to cmp eax,[num]
My guess is something is causing num, perhaps how its declared? Is always 'larger' than eax, is there extra fluff I am not aware of?
segment .bss
num: resw 1 ;For storing a number, to be read of printed....
nod: resb 1 ;For storing the number of digits....
temp: resb 2
matrix1: resw 200
m: resw 1
n: resw 1
i: resw 1
j: resw 1
small: resb 4 ; temp variable
buff resb 4
segment .data
msg1: db "Enter the number of rows in the matrix : "
msg_size1: equ $-msg1
msg2: db "Enter the elements one by one(row by row) : "
msg_size2: equ $-msg2
msg3: db "Enter the number of columns in the matrix : "
msg_size3: equ $-msg3
msg4: db "The Smallest Number is... : "
msg_size4: equ $-msg4
tab: db 9 ;ASCII for vertical tab
new_line: db 10 ;ASCII for new line
segment .text
global _start
_start:
;; code for reading number of rows and columns, this works fine.
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, msg_size1
int 80h
;; read in rows
mov ecx, 0
call read_num
mov cx, word[num]
mov word[m], cx
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, msg_size3
int 80h
;; read in columns
mov ecx, 0
call read_num
mov cx, word[num]
mov word[n], cx
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, msg_size2
int 80h
;; Reading in each element and storing it into the array
mov esi, 0
mov ebx, matrix1
;; init loop
mov word[i], 0
mov word[j], 0
;; Outer loop
i_loop:
mov word[j], 0
;; Inner Loop
j_loop:
;; A function
call read_num
;; Result of that function is now stored int he matrix
mov dx , word[num]
mov word[ebx + 2 * esi], dx
inc esi ;Incrementing array index by one....
inc word[j]
mov cx, word[j]
cmp cx, word[n]
jb j_loop
;; End Inner Loop
inc word[i]
mov cx, word[i]
cmp cx, word[m]
jb i_loop
;; End Outer Loop
;; Now begins the code to find the smallest number
mov eax, [matrix1]
;; Moves first element of Matrix1 into eax
;;; saves eax into small
mov [small], eax
;Loop through the matrix, check each number if its smaller than the first number in the array. AT the end print said number.
;Reading each element of the matrix.(Storing the elements in row major order).......
mov esi, 0
mov edi, matrix1
;; Reinit loop cters
mov word[i], 0
mov word[j], 0
;; Loop
i_loop2:
mov word[j], 0
j_loop2:
;eax will contain the array index and each element is 2 bytes(1 word) long
mov dx, word[edi+2*esi] ;
mov word[num] , dx
cmp eax,[num] ; compares eax and ebx
jle skip ;if eax is SMALLER than ebx, we can safely skip reassignment
;as our current value is already smallest.
mov eax, [num] ; stores new smallest number if the new number was smaller.
mov [small], eax
;; reassignment code is always skipped.
skip:
inc esi
inc word[j]
mov cx, word[j]
cmp cx, word[n]
jb j_loop2
inc word[i]
mov cx, word[i]
cmp cx, word[m]
jb i_loop2
; code to output the smallest number
;; Some ui text.
mov eax, 4
mov ebx, 1
mov ecx, msg4
mov edx, msg_size4
int 80h
mov ecx, 0
;; Now the actual smallest number
mov eax, 4 ; system_write
mov ebx, 1 ; stdout
mov ecx, [small] ; move smallest element to accumulator
add ecx, 48 ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff
mov edx, 4 ; size, 4 bytes
int 80h
exit:
mov eax, 1
mov ebx, 0
int 80h
;Function to read a number from console and to store that in num
read_num:
pusha
mov word[num], 0
loop_read:
mov eax, 3
mov ebx, 0
mov ecx, temp
mov edx, 1
int 80h
cmp byte[temp], 10
je end_read
mov ax, word[num]
mov bx, 10
mul bx
mov bl, byte[temp]
sub bl, 30h
mov bh, 0
add ax, bx
mov word[num], ax
jmp loop_read
end_read:
popa
ret
Oh yes, several hours of fidgeting and seconds after this post I figured out exactly why. Turns out yes, the way I declared "num" yes indeed added in extra information. I changed it to resb 4 and it works.
Crabbaskets.

Resources